0

I have a program that reads data from a file in this way

root@root# myprogram < inputfile.txt

Now I want my program to read the input file from the 3rd line and not from the beginning of the file.

I have to use < inputfile.txt. I can not call with pipe because of variable scope issues

Is there a way to do that in Linux?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268

4 Answers4

4

Maybe this will work for you (process substitution):

program < <(sed -n '3,$p' inputfile.txt)
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
3

You can use tail:

tail -n +3 inputfile.txt | myprogram

In bash, you can also use

myprogram < <(tail -n +3 inputfile.txt)
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • I have to use `< inputfile.txt`. I can not call with pipe because of variable scope issues – MOHAMED Jul 16 '14 at 13:20
  • 1
    @MOHAMED: There's always a way of solving the scope issues. Please ask specifically what you need, so we can give a comprehensive answer. – Sven Marnach Jul 16 '14 at 13:22
3

Pure shell, no extra processes:

{ read -r; read -r; program; } < inputfile.txt

The first two calls to read each consume a line of input from input file.txt, so that they are not seen by program.


You can generalize this to skip the first $n lines of input.

{ 
  while [ "$((i++))" -lt "$n" ]; do read -r; done
  program
} < inputfile.txt

This becomes a little more readable with the use of some bash extensions:

{ while (( i++ < n )); do read -r; done; program; } < inputfile.txt
chepner
  • 497,756
  • 71
  • 530
  • 681
0

try this command: sed -n '3,$p' inputfile.txt | myprogram

zhujs
  • 543
  • 2
  • 12