Awk - New Line between columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk - New Line between columns
# 1  
Old 12-03-2009
Awk - New Line between columns

I have a data file with 4 columns, of the format:
Code:
A1 A2 A3 A4
B1 B2 B3 B4
C1 C2 C3 C4
etc..

I would like to insert to put column 2,3,4 on a new line so my new format would be:

Code:
A1
A2 A3 A4
B1
B2 B3 B4
C1
C2 C3 C4 
etc.

but am new at using AWK and am not sure how to do it.

Thanks.

Last edited by RavinderSingh13; 11-07-2019 at 10:14 AM..
# 2  
Old 12-03-2009
Hi,

Code:
 awk '{print $1"\n"$2,$3,$4}' file

# 3  
Old 12-03-2009
Thanks for the quick reply - I didn't know you needed quote marks around the \n . That's why it didn't work for me.

---------- Post updated at 12:51 PM ---------- Previous update was at 12:30 PM ----------

Is it also possible to modify my output date file so it also contains the initial row numbers of the data lines
e.g. the original data file goes to-->

A1 1
A2 A3 A4
B1 2
B2 B3 B4
C1 3
C2 C3 C4

etc.

Thanks.
# 4  
Old 12-03-2009
This should do the trick
Code:
awk '{print $1,NR "\n" $2,$3,$4}' file

# 5  
Old 12-03-2009
Code:
awk '{print $1" "NR"\n"$2,$3,$4}' urfile

# 6  
Old 12-03-2009
Thanks for that.

I've got one final question. Now that I've got the data in the format above I want to search the file for certain strings (These will always be in the *1 field) and then add a number to the end of that line based on the string.
i.e the output file will now look like

A1 1 x
A2 A3 A4
B1 2 y
B2 B3 B4
C1 3 z
C2 C3 C4

where x,y,z are based on the values A1, B1, C1 respectively (A1,B1,C1 are all letters and are the only letters that appear in the file - the rest are numbers). I don't need anything complicated that will take values from a lookup table or anything like that. Just a statement that I can change the individual parameters on.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

Split multi columns line to 2 columns

I have data like this 1 a,b,c 2 a,c 3 b,d 4 e,f would like convert like this 1 a 1 b 1 c 2 a 2 c 3 b 3 d 4 e 4 f Please help me out (4 Replies)
Discussion started by: jhonnyrip
4 Replies

3. Shell Programming and Scripting

awk to copy previous line matching a particular columns

Hello Help, 2356798 7689867 999 000 123678 20385907 9797 666 17978975 87468976 968978 98798 I am trying to have out put which actually look for the third column value of 9797 and then it insert line there after with first, second column value exactly as the previous line and replace the third... (3 Replies)
Discussion started by: Indra2011
3 Replies

4. Shell Programming and Scripting

awk : Want to print columns of different input files in line wise

Hi, I want to print last column of 3 input files with awk. cat file1.txt file2.txt file3.txt file1.txt 1 ad 200 2 ss 300 file2.txt 1 mm 444 2 ee 555 file3.txt 1 kk 999 2 jj 555 My o/p should be :- 1 200 444 999 2 300 555 555 (3 Replies)
Discussion started by: satishmallidi
3 Replies

5. UNIX for Dummies Questions & Answers

Arranging columns in a line

Hi, Please give a solution... Current File 200101701 A 5 B 283 D 222 200085506 A 5 B 6 C 304 200081406 A 5 200101784 D 1111Desired Output 200101701 A 0005 B 0283 * * D 222 ... (4 Replies)
Discussion started by: satyar
4 Replies

6. Shell Programming and Scripting

Summing columns in line

I have a file with the following format AAAAA 1.34B 0.76B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.90B 0.00B 0.00B 0.46B 0.00B 0.03B 0.00B ... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

7. UNIX for Dummies Questions & Answers

Awk match on columns and delete line

Hi, I have a file like this a 1 2 b 2 2 c 2 3 d 4 5 f 5 6 output a 1 2 c 2 3 d 4 5 f 5 6 Basically, I want to delete the whole line if $2 and $3 are the same. Thanks (5 Replies)
Discussion started by: jacobs.smith
5 Replies

8. Shell Programming and Scripting

awk compare 2 columns, 2 files, output whole line

Hello, I have not been able to find what I'm looking for via searching the forum. I could use some help with an awk script or one-liner to solve this simple problem. I have two files. If $1 and $2 from file1 match $1 and $2 from file2, print the whole line from file2. Example file1 ... (2 Replies)
Discussion started by: jm4smtddd
2 Replies

9. UNIX for Dummies Questions & Answers

Averaging 100 columns together line by line

Hi I have 100 xy graphs and need to average these together in a line by line fashion. The value of the x axis are the same. y differs e.g. taking only 2 graphs: graph 1 x y 1 3 2 5 3 7 4 9 5 11 graph 2 x y 1 4 2 6 3 10 (2 Replies)
Discussion started by: jenjen_mt
2 Replies

10. Shell Programming and Scripting

using command line arguments as columns for pattern matching using awk

Hi, I wish to use a column, as inputted by a user from command line, for pattern matching. awk file: { if($1 ~ /^8/) { print $0> "temp2.csv" } } something like this, but i want '$1' to be any column as selected by the user from command line. ... (1 Reply)
Discussion started by: invinclible0009
1 Replies
Login or Register to Ask a Question