Getting fields from different line..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting fields from different line..
# 1  
Old 08-02-2012
Getting fields from different line..

input:

Code:
field1
field2
field3 field4

output:

Code:
"field1";"field2";"field3";"field4"

My input file is shown above and I want output as shown using awk. the length of field3 is fixed so, the space between field3 and field4 can vary according to the value of field3. I am new to unix, so can anyone please help me.....


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-02-2012 at 04:56 AM.. Reason: code tags
# 2  
Old 08-02-2012
Code:
awk -v RS="" '{for(i=1;i<=NF;i++)printf("\"%s\";",$i)}' input.txt

# 3  
Old 08-02-2012
thanx itkamaraj......that was very helpful...
but if there are multiple records like..
i/p
Code:
field1
field2
field3 field4
<blank row>
field1
field2
field3 field4
<blank row>

o/p
Code:
"field1";"field2";"field3";"field4"
"field1";"field2";"field3";"field4"

how to get this???
than in advance..previous solution was really helpful...

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-02-2012 at 04:56 AM.. Reason: code tags, see PM
# 4  
Old 08-02-2012
Code:
 
$ cat input.txt
field1
field2
field3 field4
 
field1
field2
field3 field4
 
 
$ awk '{for(i=1;i<=NF;i++)printf("\"%s\";",$i)}NF==0{printf("\n")}' input.txt
"field1";"field2";"field3";"field4";
"field1";"field2";"field3";"field4";

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 08-02-2012
thanx.....

---------- Post updated at 06:31 PM ---------- Previous update was at 01:41 PM ----------

hi itkamaraj...i am new to unix, can you plz explain both the code......
# 6  
Old 08-03-2012
Code:
 
for(i=1;i<=NF;i++) - NF means no. of fields. so this for loop iterate the fields in the line
printf("\"%s\";",$i) - Print the field value along with ";"
NF==0 - if it is empty row print new line

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search every line and compare fields

Hi All, I have situation where my file looks like this log.file. ID Start time IP SQL 1256152 05-Aug-15, 11:25:06 MST 10.54.20.33 Select * from TES 1004768 05-Aug-15, 11:25:06 MST 10.54.20.33 Select dummy 323323 05-Aug-15, 12:00:06 MST 10.15.20.77 ... (1 Reply)
Discussion started by: netdbaind
1 Replies

2. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

3. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

4. Shell Programming and Scripting

Compare fields in two files line by line

I am new to awk scripting. I want to do a field by word (field) comparison of two files File1.txt and File2.txt. The files contain a list of | (pipe) separated field. **File 1: ------------------- aaa|bbb|ccc|eee|fff lll|mmm|nnn|ooo|ppp rrr|sss|ttt|uuu|vvv** File 2: ... (7 Replies)
Discussion started by: dhruvmohan
7 Replies

5. Shell Programming and Scripting

Count blank fields in every line

Hello All, I am trying a one liner for finding the number of null columns in every line of my flat file. The format of my flat file is like this a|b|c|d||||e|f|g| a|b|c|d||||e|f|g| I want to count the number of fields delimited by "|" which are blank. In above case the count should be... (6 Replies)
Discussion started by: nnani
6 Replies

6. UNIX for Dummies Questions & Answers

to get fields from different line to a single line

input: field1 field2 field3 field4 output: "field1";"field2";"field3";"field4" My input file is shown above and I want output as shown using awk. the length of field3 is fixed so, the space between field3 and field4 can vary according to the value of field3. I am new to unix, so... (1 Reply)
Discussion started by: dreamzalive
1 Replies

7. Shell Programming and Scripting

convert fields on each line to one line

Hello, I have a text file with the following fields: RecordNumber Name Email Date Notes Confirmed MailingList The problem is that each field is on a separate line. These seven fields repeat for the length of the file. What I want to do is to have a bash script or similar unitil EOF... (6 Replies)
Discussion started by: vestport
6 Replies

8. Shell Programming and Scripting

print last 2 fields of a line

Hi everyone, happy new year! Can someone teach me how I can print the last 2 fields of every line in a file (lines don't have uniform number of fields), for example: input file: aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk lll mmm nnn ooo output: ccc ddd fff ggg iii jjj nnn ooo ... (2 Replies)
Discussion started by: Deanne
2 Replies

9. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

10. UNIX for Dummies Questions & Answers

sorting fields of a line

Hi all, I have a file that looks like this... ########## 1zz2_15-43 1ouy_0-13-35 1.12619901947 2gfs_41-7 1yqj_3 0.793602121208 1bl7_11-3 1wbo_1-3-4 0.791065168287 1ywr_16-3 2ghl_22 0.956896171134 2exc_11-35 1pmq_13-15-87 0.597677672501 2bal_25-7 1ouk_17-19-21-228-58 0.668388304836... (6 Replies)
Discussion started by: Digby
6 Replies
Login or Register to Ask a Question