Operating System - HP-UX
1751894 Members
5357 Online
108783 Solutions
New Discussion юеВ

Re: add line number file (combination with xargs)

 
SOLVED
Go to solution
Billa-User
Regular Advisor

add line number file (combination with xargs)

hello,
i have following questions :

- what is the fastest (or easiest) way to add at the begin of file the line number ?
my solutions :
awk '{ print $NR,$0 }' file > file_temp
or
nl -w1 -s" " file > file_temp
and
mv file_temp file
better solutions ?

- how to change this part of a shell script :
xargs -l -x ./sub_prog < xargs_params_file
to
- i want to add a the begin of "xargs_params_file" the line number
i think i can only do this to modify "xargs_params_file" ? a combination is possible

best regards,
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: add line number file (combination with xargs)

Hi:

Either of your solutions to add a line number works. In order to eliminate the manual move of the updated file, you could use Perl:

# perl -ni.old -e 'print "$. $_"' myfile

...which performs an inplace update and preserves a copy of the original file as "*.old".

As for your second question, if I understand correctly you could do something like:

# nl xargs_parms_file|cat -- - myfile

Note the '--' to signal the end of any options and a '-' to denote STDIN.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: add line number file (combination with xargs)

>JRF: nl xargs_parms_file | cat -- - myfile
>Note the '--' to signal the end of any options and a '-' to denote STDIN.

I'm not sure why you would want to use an evil cat? And cat(1) accepts the "-" without the "--".

Note: You need to read the documentation for nl(1) carefully. By default it won't number empty lines, so you need to add -ba.
Billa-User
Regular Advisor

Re: add line number file (combination with xargs)

>> perl -ni.old -e 'print "$. $_"' myfile

thank's for this solution

>> xargs -l -x ./sub_prog < xargs_params_file

maybe i explained it not so clear. in "xargs_params_file" are different entries (sep. " ") in each line and i don't know how many lines will exist in "xargs_params_file". for a change in "./sub_prog" in want to add as first parameter the line number of "xargs_params_file".

so i think i have to change "xargs_params_file" ?

or a combination of shell command's can solve this case ?

so

>> nl xargs_parms_file|cat -- - myfile

solve

>> xargs -l -x ./my_shell_sub_programm < xargs_params_file

??
Dennis Handly
Acclaimed Contributor
Solution

Re: add line number file (combination with xargs)

>in xargs_params_file are different entries (sep. " ") in each line and I don't know how many lines will exist in xargs_params_file. for a change in ./sub_prog in want to add as first parameter the line number of "xargs_params_file".

>I think I have to change "xargs_params_file"?

Yes, you can use nl(1) to number each line and have sub_prog use that first argument

>> nl xargs_parms_file|cat -- - myfile
>solve
>> xargs -l -x ./my_shell_sub_programm < xargs_params_file

You could do:
nl -ba xargs_parms_file | xargs -l -x ./my_shell_sub_programm
Billa-User
Regular Advisor

Re: add line number file (combination with xargs)

@ nl -ba xargs_parms_file | xargs -l -x ./my_shell_sub_programm

perfect,perfect. thx a lot.
sorry for my delayed answer.