Convert first line into column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Convert first line into column
# 1  
Old 07-16-2009
Tools Convert first line into column

I have a file:

Code:
2009_11
3455	500
5355	600
7777	700

The first line is the period. I want to concatenate the Period into every line using a one line command..I guess maybe awk or something. So the output would look like this:

Code:
3455	500  2009_11
5355	600  2009_11
7777	700  2009_11

Note that the column will need to be added such that the file continues to be a tab delimited file.

How would I do this?
# 2  
Old 07-16-2009
Code:
nawk 'FNR==1 {line1=$0;next} {print $0 "\t" line1}' myFile

# 3  
Old 07-16-2009
Unix.com still rocks!! nawk didnt work coz my box didnt have it...but I tried it with awk and it works perfectly!

Thanks much...if it is not too much of an effort do you care to explain?
# 4  
Old 07-17-2009
Quote:
Originally Posted by alfredo123
Unix.com still rocks!! nawk didnt work coz my box didnt have it...but I tried it with awk and it works perfectly!

Thanks much...if it is not too much of an effort do you care to explain?
NFR contains the number of fields read so far. In pseudo code:

Code:
loop: if NFR equals to 1 then
  line1 = line_we_just_read
  goto loop
end
print line_we_just_read line1

Hence, the assignment to line1 is carried out only once and the print code is executed always except at the first line.

Cheers,

pen
# 5  
Old 07-19-2009
Try this:
Code:
xx=`head -1  test.txt` && sed -n "{1! s/\(.*\)/&\t$xx/p;}" test.txt
 
Or 
 
xx=`head -1  test.txt`
sed -n "{1! s/\(.*\)/&\t$xx/p;}" test.txt

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a column to a line

Hi, I need to convert my text file into a sort of matrix form. my input file has 5 columns. $2 and $4 will be ignored and omitted from output, while $3 need to be re-arranged horizontally on top:- Akd1 aa233 Akd1 545 524.2 jlk01 aa233 Akd1 90 447 mht5 ... (2 Replies)
Discussion started by: redse171
2 Replies

2. Shell Programming and Scripting

Convert Rows into Column

Hi Experts, I have a requirement to convert rows into columns. For e.g. Input File: Output File should be like Appreciate if you could suggest code snippet(may be awk) for above requirement... Thanks in Advance for your help... (3 Replies)
Discussion started by: sai_2507
3 Replies

3. Shell Programming and Scripting

Merge two files line by line and column by column

Hi All, I have two files having oracle query result. I want to merge to files line by line and also with column File1 23577|SYNC TYPE 23578|Order Number|ConnectionState 23585|Service State|Service NameFile2 23577|AR Alarm Sync 23578|A5499|9 23585|7|test_nov7Result... (18 Replies)
Discussion started by: Harshal22
18 Replies

4. Shell Programming and Scripting

convert row to column with respect of first column.

Input file A.txt :- C2062 -117.6 -118.5 -117.5 C5145 0 0 0 C5696 0 0 0 Output file B.txt C2062 X -117.6 C2062 Y -118.5 C2062 Z -117.5... (4 Replies)
Discussion started by: asavaliya
4 Replies

5. Shell Programming and Scripting

convert single line output to multiple line

Hi all, I have a single line output like below echo $ips 10.26.208.28 10.26.208.26 10.26.208.27 want to convert above single line output as below format. Pls advice how to do ? 10.26.208.28 10.26.208.26 10.26.208.27 Regards Kannan (6 Replies)
Discussion started by: kamauv234
6 Replies

6. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

7. Shell Programming and Scripting

Convert string in a column

Hi I got this: aix.acct,aix.system.config.cron,aix.system.config.src,aix.system.install,string2 and I want this: aix.acct system.config.cron aix.system.config.src aix.system.install string2 I tried using sed by changing ',' into an 'ENTER' but I couldn't. :-( thxs (2 Replies)
Discussion started by: iga3725
2 Replies

8. Shell Programming and Scripting

Convert Column to rows

Hi, I have a file with below contents. Heading1 Heading2 Heading3 Heading4 Value1 Value2 Value3 Value4 The file has only 2 rows and is tab separated The desired output is : Heading1 Value1 Heading2 Value2 Heading3 Value3 Heading4 Value4 CAn you please help? (5 Replies)
Discussion started by: kaponeh
5 Replies

9. Shell Programming and Scripting

awk convert from line to column

i have an output like this : 012008 25760883 022008 12273095 032007 10103 032008 10115642 042007 20952798 but i would like to have it like this 012008,25760883 022008,12273095 032007,10103 032008,10115642 042007,20952798 (4 Replies)
Discussion started by: jarmouda
4 Replies

10. UNIX for Dummies Questions & Answers

read a line from a csv file and convert a column to all caps

Hello experts, I am trying to read a line from a csv file that contains '.doc' and print the second column in all caps. e.g. My csv file contains: Test.doc|This is a Test|test1|tes,t2|test-3 Test2.pdf|This is a Second Test| test1|tes,t2|t-est3 while read line do echo "$line" |... (3 Replies)
Discussion started by: orahi001
3 Replies
Login or Register to Ask a Question