Operating System - Linux
1830940 Members
1728 Online
110017 Solutions
New Discussion

Interesting Shell Script Requirement

 
SOLVED
Go to solution
Anurag_7
Advisor

Interesting Shell Script Requirement

There is a command for which the output is as follows

-----------------------------------
1
2
3
4 5 6 7
8 9 10 11
12 13 14 15
16 17 18 19

-----------------------------------

The requirement is to extract information from the 4th line and write it to a file as comma separated values 4, 5, 6, 7

And similary for the next two lines(each line goes to a different file.)

Is there a good way to do this?

Any help in this regard would be highly appreciated.

Regards,

Anurag
9 REPLIES 9
Ivan Ferreira
Honored Contributor

Re: Interesting Shell Script Requirement

Maybe something like this:

awk 'NR==$2 {print $0}' $1 | tr " " "," > $3

Usage:

script.sh line_numb input_file output_file
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Peter Godron
Honored Contributor

Re: Interesting Shell Script Requirement

Anurag,
assuming your initial data file is called a.dat. Also that there are no trailing spaces.
This command translates all spaces into commas and writes the new data to file b.dat.

tr " " "," < a.dat > b.dat

will create

1
2
3
4,5,6,7
8,9,10,11
12,13,14,15
16,17,18,19
Kent Ostby
Honored Contributor

Re: Interesting Shell Script Requirement

create a file called "pull.awk" which looks like this:

NR<7&&NR>3 {for (idx=1;idx=NF;idx++) {printf("%s,",$idx);}
printf("\n");}

awk -f pull.awk < inputfile > outputfile

That takes care of the comma seperated part.

What is the file name syntax that you are using for each file?
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Enrico P.
Honored Contributor

Re: Interesting Shell Script Requirement

Hi,
you can try with:

head -n n_of_line file_source|tail -1|sed 's/ /,/g' > file_dest

Enrico
Rodney Hills
Honored Contributor
Solution

Re: Interesting Shell Script Requirement

Here is something that will generate the individual files. 1 per line starting with the fourth line of your input file-

awk 'NR>3{gsub(" ",",");print $0}' yourdata.dat | split -l1 - outfil

This will generate files outfilaa, outfileab, etc. One file per line.

HTH

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: Interesting Shell Script Requirement

I'm looking for the interesting requirement; none of this interests me in the least.
If it ain't broke, I can fix that.
Peter Nikitka
Honored Contributor

Re: Interesting Shell Script Requirement

Hi,

yourcommand |
awk -v OFS=, 'NR>3 {out="/tmp/outf"++x;for (i=1;iout;print $NF >out;close(out)}'

will produce files /tmp/outf* .

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Anurag_7
Advisor

Re: Interesting Shell Script Requirement

Thanks a lot Iva, Peter Godron, Kent, Enrico, Rodney, Peter Niktka......

All the solution were damn good......

I will handle the naming convention for the output files myself.

Thanks a lot again

Anurag
Anurag_7
Advisor

Re: Interesting Shell Script Requirement

Solution Found with the help of other forum members.