Convert header rows into


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert header rows into
# 1  
Old 02-28-2014
Code Convert header rows into

I want to put the 3 first lines into a single line separated by ;
I've tried to use Sed and Awk but without success. I'm new to Shell scripting.
Thanks in advance!

Input
Code:
112
DESAC_201309_OR_DJ10
DJ10
1234567890123;8
1234567890124;20
1234567890125;3

expected Output
Code:
112;DESAC_201309_OR_DJ10;DJ10
1234567890123;8
1234567890124;20
1234567890125;3


Last edited by MoroccanRoll; 02-28-2014 at 08:28 PM.. Reason: quote tags changed to code tags
# 2  
Old 02-28-2014
Based on some assumptions:
Code:
awk '!/\;/{s=s?s";"$0:$0;next}/\;/{$0=s?s"\n"$0:$0;s=""}1' file

# 3  
Old 02-28-2014
Code:
awk '{ORS=(NR<3)?";":"\n"}1' file

# 4  
Old 02-28-2014
Try also
Code:
awk 'NR<3{printf "%s;",$0;next}1' file

# 5  
Old 02-28-2014
Code:
sed '1{N;N;s/\n/;/g;}' file

or
Code:
sed '1N;2N;3s/\n/;/g' file

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-03-2014
Thank you all, It all worked very fine.
There have been a slight modification on the expected data.
I need ";" at the end of each line

Quote:
112;DESAC_201309_OR_DJ10;DJ10;
1234567890123;8;
1234567890124;20;
1234567890125;3;
---------- Post updated at 06:15 AM ---------- Previous update was at 05:58 AM ----------

I found the solution Smilie

Code:
 sed '1N;2N;3s/\n/;/g;s/.*/&;/' file

# 7  
Old 03-03-2014
Good. Just for fun, you could shorten it a tiny bit with standard sed:
Code:
sed '1N;2N;3s/\n/;/g;s/$/;/' file


Last edited by Scrutinizer; 03-03-2014 at 07:59 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Append pipe | at the end of all the rows except header n trailer for all the files under a directory

Hi Experts Need help... I am looking for a Unix script to append pipe | at the end of all the rows (except header and trailer)in all the files placed under the directory /interfaces/Temp e.g. Header row1 row2 row3 Trailer The script should read all the files under... (3 Replies)
Discussion started by: phani333
3 Replies

2. Shell Programming and Scripting

Convert rows into column along with header

Hi, I have a requirement to format the data in a new order. Here is my source format : ppp ***Wed Dec 16 10:32:30 GMT 2015 header1 header2 header3 header4 header5 server1 0.00 0.02 0.07 0.98 server2 0.01 0.00 0.08 0.79 server3 0.05 0.82 0.77 0.86 ... (18 Replies)
Discussion started by: john_prince
18 Replies

3. UNIX for Beginners Questions & Answers

Keep only columns in first two rows based on partial header pattern.

I have this code below that only prints out certain columns from the first two rows (doesn't affect rows 3 and beyond). How can I do the same on a partial header pattern “G_TP” instead of having to know specific column numbers (e.g. 374-479)? I've tried many other commands within this pipe with no... (4 Replies)
Discussion started by: aachave1
4 Replies

4. Shell Programming and Scripting

Match in awk skipping header rows

I am trying to match $1-$7 between the two files and if a match is found then the contents of $8 in file2 and copied over. The awk I tried is below. There is also a header row in file2 that has the Chr Start End Ref Alt that does not need to be searched. Thank you :). awk awk... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Convert rows to column and add header

Hi, I need help to convert rows in input file into a table. inputfile 192.98.1 192.98.192.98.17 VVC family Zorro 10 192.98.1 192.98.192.98.17 VVC family Ace 1 192.98.1 192.98.192.98.17 VVC family ... (4 Replies)
Discussion started by: redse171
4 Replies

6. Shell Programming and Scripting

Manipulate all rows except header, but header should be output as well

Hello There... I have a sample input file .. number:department:amount 125:Market:125.23 126:Hardware store:434.95 127:Video store:7.45 128:Book store:14.32 129:Gasolline:16.10 I will be doing some manipulations on all the records except the header, but the header should always be... (2 Replies)
Discussion started by: juzz4fun
2 Replies

7. UNIX and Linux Applications

Script to delete few rows from a file and then update header

HJKL1Name00014300010800000418828124201 L201207022012070228XAM 00000000031795404 001372339540000000000000000000000 COOLTV KEYA Zx00 xI-50352202553 00000000 00000000 G000000000000 00000000 ... (10 Replies)
Discussion started by: mirwasim
10 Replies

8. Shell Programming and Scripting

Convert Header into Column in Text file

Hi Gurus, I have a requirement like this and have to create a UX shell scripts. Thanks in advance. File-in: ------ Header2007-12-012007-11-21 100|xyz|was 101|wsa|qws ...... ....... Output should be: ------------------- 2007-12-01|100|xyz|was 2007-12-01|101|wsa|qws ...... .......... (7 Replies)
Discussion started by: vsubbu1000
7 Replies

9. Shell Programming and Scripting

remove header and footer rows

I would like to remove some lines from begining of file (header) and some lines from end of file (footer). The header/footer lines generated by web-browser when the user upload a file to my webserver. Example: -----------------------------7d62af20c052c Content-Disposition: form-data;... (2 Replies)
Discussion started by: seaky
2 Replies
Login or Register to Ask a Question