convert fields on each line to one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting convert fields on each line to one line
# 1  
Old 07-16-2012
convert fields on each line to one line

Hello,

I have a text file with the following fields:

Code:
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 read each of the above 7 lines into a variable and then write out each variable to another file delimited by a ";".

Example:
Code:
1
John Smith
john@mymail.com
2012-07-07 10:00:00
No notes
Yes
Testlist
2
Jane Brady
jane@bradynetworks.com
2012-04-07 10:00:00
No notes
Yes
Testlist

The above should look like the following in the new file:

Code:
1;John Smith;john@mymail.com;2012-07-07 10:00:00;No notes;Yes;Testlist
2;Jane Brady;jane@bradynetworks.com;2012-04-07 10:00:00;No notes;Yes;Testlist

Some fields are blank so not sure if that is an issue.

Thanks in advance for any replies!


Art

Last edited by Scott; 07-17-2012 at 04:54 AM.. Reason: Code tags
# 2  
Old 07-16-2012
Have a look at the paste man page. It can do this for you (including handling empty fields/lines). If you need further help, just let us know what you don't understand about what you read and the command you tried.

Hint: You will need to use - multiple times to read from standard input.

Regards,
Alister
# 3  
Old 07-16-2012
Problem Solved!

Thank you Alister for pointing me in the right direction. I have used paste before but never looked at the "-" function. I ended up with:

Code:
 < infile paste -d";" - - - - - - - | awk '{print $1$2$3$4$5$6$7}'

I couldn't figure out how I could do it without piping it through awk but anyways problem solved!

Thanks so much for your reply!


ArtSmilie

---------- Post updated at 08:20 PM ---------- Previous update was at 08:08 PM ----------

Sorry the correct script is:

Code:
< infile paste -d";" - - - - - - - | awk '{print $1$2,$3,$4$5$6$7$8$9}'

The only problem I have is that the second field "name" may or may not have a middle initial. This causes a leading space if the name does not include a middle initial. I am going to try to modify the infile first and then paste with the "-". I will post any updates in the hopes that this may help someone else.


Art

Last edited by Scott; 07-17-2012 at 04:54 AM.. Reason: Use coder tags, please...
# 4  
Old 07-16-2012
I don't understand why you are using awk. The output from paste should match the desired output presented in your original post.

Regards,
Alister
# 5  
Old 07-17-2012
Bug No awk

Not sure how. Just doing the best that I can with what I know. If you have a better way then by all means post it.

Thanks again!


Art
# 6  
Old 07-17-2012
Code:
paste -sd';;;;;;\n' infile

# 7  
Old 07-17-2012
Still not exactly what I need

I appreciate your input and I had no idea you could use paste like this too so thanks so much for that. The results put everything with a tab after the delimiter. I want all fields concatenated but just not the names themselves concatenated like the example I had:

Code:
1;John Smith;john@mymail.com;2012-07-07 10:00:00;No notes;Yes;Testlist; 
2;Jane Brady;jane@bradynetworks.com;2012-04-07 10:00:00;No notes;Yes;Testlist;

Actually I would like to eliminate the first field too (the record numbers portion - 1, 2 etc.). The last field should have a trailing ";". Sorry I didn't say that before. Even if I take what you had and run it through awk email addresses will have a leading space if the previous name field did not use a middle initial.

Art

Last edited by vestport; 07-18-2012 at 09:26 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 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

Repeat line convert upper line

HI Guys, My Input :- R3 AV44 50 0 100 100 100 R3 AV44 1 0 100 100 100 R3 AV45 50 0 100 100 100 R3 AV45 0 3 100 100 100 R3 AV45S 50 0 100 100 100 R3 AV45S 0 4 100 100 100 Output :- R3 AV44 50 0 100 100 100 1 0 100 100 100 R3 AV45 50 0 100 100 100 0 3 100 100 100 R3 AV45S 50 0... (9 Replies)
Discussion started by: pareshkp
9 Replies

4. 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

5. 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

6. 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

7. Shell Programming and Scripting

Getting fields from different 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 can... (5 Replies)
Discussion started by: dreamzalive
5 Replies

8. 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

9. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

10. 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
Login or Register to Ask a Question