awk/sed script to print each line to a separate named file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk/sed script to print each line to a separate named file
# 1  
Old 07-25-2011
awk/sed script to print each line to a separate named file

I have a large 3479 line .csv file, the content of which looks likes this:

Code:
1;0;177;170;Guadeloupe;x
2;127;171;179;Antigua and Barbuda;x
3;170;144;2;Umpqua;x
4;170;126;162;Coos Bay;x
...
1205;46;2;244;Unmak Island;x
1206;47;2;248;Yunaska Island;x
1207;0;2;240;north sea;x
1208;48;2;236;atlantic ocean;x
...
3477;1;40;236;Lake Erie;x
3478;2;40;240;Lake Ontario;x
3479;3;40;244;Lake Winnipeg;x

What I need to do is print each line to a seperate unique .txt file.

The filename of each file must be in a particular format based on the contents of each line, for example "1 - Guadeloupe.txt" for the first file.

The contents of every file should be the same and is simply two lines:
trade_goods = timber
life_rating = 15

This needs to be accomplished using GNU command line tools such as sed and gawk. I am new to this.

I appreciate any help anybody can give me. Thank you.

Last edited by radoulov; 07-25-2011 at 08:59 AM.. Reason: Code tags, please!
# 2  
Old 07-25-2011
Tools gawk

Quote:
Originally Posted by kalelovil
I have a large 3479 line .csv file, the content of which looks likes this:

Code:
1;0;177;170;Guadeloupe;x
2;127;171;179;Antigua and Barbuda;x
3;170;144;2;Umpqua;x
4;170;126;162;Coos Bay;x
...
1205;46;2;244;Unmak Island;x
1206;47;2;248;Yunaska Island;x
1207;0;2;240;north sea;x
1208;48;2;236;atlantic ocean;x
...
3477;1;40;236;Lake Erie;x
3478;2;40;240;Lake Ontario;x
3479;3;40;244;Lake Winnipeg;x

What I need to do is print each line to a seperate unique .txt file.

The filename of each file must be in a particular format based on the contents of each line, for example "1 - Guadeloupe.txt" for the first file.

The contents of every file should be the same and is simply two lines:
trade_goods = timber
life_rating = 15

This needs to be accomplished using GNU command line tools such as sed and gawk. I am new to this.

I appreciate any help anybody can give me. Thank you.

Hi,

Try this one.
Code:
gawk 'BEGIN{FS=";";}{split($0,a,";");gsub(/ /,"",a[NF-1]); print $0 >a[NF-1];}'  input_file


Cheers,
RangaSmilie

Last edited by Franklin52; 07-25-2011 at 05:24 PM.. Reason: Please use code tags for data and code samples, thank you
# 3  
Old 07-25-2011
Its always good to have file names without spaces/blanks.
Code:
awk -F';' '{print "trade_goods = timber\nlife_rating = 15" > $1"-"$5".txt"}' inputfile

# 4  
Old 07-25-2011
Tools gawk

yes obsolutely right.

the above code will remove white spaces.
Code:
gawk 'BEGIN{FS=";";}{split($0,a,";");gsub(/ /,"",a[NF-1]); print $0 >a[NF-1];}' input_file


Cheers,
RangaSmilie

Last edited by Franklin52; 07-25-2011 at 05:24 PM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 07-25-2011
Code:
# x=1;for((i=1;i<$(sed -n '$=' infile);i++)); do fname=$(sed -ne "s/^\([^;]*\).*;\([^;]*\);x/\1 - \2/;$x s/ //g" -e "$x s/.*/&.txt/p" infile); echo -e "trade_goods = timber\nlife_rating = 15">"$fname"; ((x++)); done

regards
ygemici
# 6  
Old 07-25-2011
Code:
v="trade_goods = timber
life_rating = 15"

awk -F\; '{f=$1"-"$5; print v>f; close(f)}' v="$v" infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

3. UNIX for Dummies Questions & Answers

Split Every Line In Txt Into Separate Txt File, Named Same As The Line

Hi All Is there a way to export every line into new txt file where by the title of each txt output are same as the line ? I have this txt files containing names: Kandra Vanhooser Rhona Menefee Reynaldo Hutt Houston Rafferty Charmaine Lord Albertine Poucher Juana Maes Mitch Lobel... (2 Replies)
Discussion started by: Nexeu
2 Replies

4. Programming

Read text from file and print each character in separate line

performing this code to read from file and print each character in separate line works well with ASCII encoded text void preprocess_file (FILE *fp) { int cc; for (;;) { cc = getc (fp); if (cc == EOF) break; printf ("%c\n", cc); } } int main(int... (1 Reply)
Discussion started by: khaled79
1 Replies

5. Shell Programming and Scripting

awk print header as text from separate file with getline

I would like to print the output beginning with a header from a seperate file like this: awk 'BEGIN{FS="_";print ((getline < "header.txt")>0)} { if (! ($0 ~ /EL/ ) print }" input.txtWhat am i doing wrong? (4 Replies)
Discussion started by: sdf
4 Replies

6. Shell Programming and Scripting

Passing parameter in sed or awk commands to print for the specific line in a file

Hi, I am trying to print a specific line in a file through sed or awk. The line number will be passed as a parameter from the previous step. My code looks as below. TEMP3=`sed -n '$TEMP2p' $FILEPATH/Log.txt` $TEMP2, I am getting from the previous step which is a numerical value(eg:3). ... (2 Replies)
Discussion started by: satyasrin82
2 Replies

7. Shell Programming and Scripting

extract nth line of all files and print in output file on separate lines.

Hello UNIX experts, I have 124 text files in a directory. I want to extract the 45678th line of all the files sequentialy by file names. The extracted lines should be printed in the output file on seperate lines. e.g. The input Files are one.txt, two.txt, three.txt, four.txt The cat of four... (1 Reply)
Discussion started by: yogeshkumkar
1 Replies

8. Shell Programming and Scripting

awk script: print line number n of another file

Hi, I wrote an awk script to analyse file A. I call the script with files A and B. File A has lines like: 000000033100001 000000036100001 000000039100001 The first 9 characters are interpreted as a line number; for each line number found I want to output this line number of file B. ... (13 Replies)
Discussion started by: kpg
13 Replies

9. Shell Programming and Scripting

awk/shell script to print each line to a file

Dear People, My query is: have a file, which looks likes this: 10 20 30 40 50 1 2 3 4 5 100 200 300 400 500 what i need is: "PRINT EACH LINE TO AN UNIQUE FILE" desired output: file 1 10 20 30 40 50 file 2 1 2 3 4 5 (3 Replies)
Discussion started by: saint2006
3 Replies

10. Shell Programming and Scripting

To parse through the file and print output using awk or sed script

suppose if u have a file like that Hen ABCCSGSGSGJJJJK 15 Cock ABCCSGGGSGIJJJL 15 * * * * * * : * * * . * * * : Hen CFCDFCSDFCDERTF 30 Cock CHCDFCSDHCDEGFI 30 * . * * * * * * * : * * :* : : . The output shud be where there is : and . It shud... (4 Replies)
Discussion started by: cdfd123
4 Replies
Login or Register to Ask a Question