Convert Header into Column in Text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Header into Column in Text file
# 1  
Old 08-01-2007
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
......
.......

pls suggest.Thanks again.
# 2  
Old 08-01-2007
Code:
#!/bin/ksh

InpFile=$1
OutFile="$HOME/my_output.txt"
pattern=$(head -1 $1 | sed 's/Header\([0-9-]\{10\}\)[0-9-]*/\1/');
sed "/Header/d;s/^/$pattern|/g" $InpFile > $OutFile

# 3  
Old 08-01-2007
Hi vsubbu1000, the "Contact Admins and Mods" forum description says:

"Post Here to Contact Site Administrators and Moderators Make Suggestions About Forums, Features, or Content here. Discuss Rules and Guidelines. Get Forum Support Here. (Registered Users Only)"

Do not post any queries regarding shell scripting/normal unix questions in that forum. For such questions, please use the other subforums available.
# 4  
Old 08-01-2007
Hi Lorcan,

Output comes as:
-------------------
Header2007-12-012007-11-21|100|xyz|was
Header2007-12-012007-11-21|101|wsa|qws

but it should be:
--------------
2007-12-01|100|xyz|was
2007-12-01|101|wsa|qws

any idea?Thanks!
# 5  
Old 08-01-2007
i am not sure as to why that happens, it works fine for me
try the below code

Code:
#!/bin/ksh

InpFile=$1
OutFile="$HOME/my_output.txt"
pattern=$(head -1 $1 | sed 's/Header\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)[0-9-]*/\1/' );
sed "/Header/d;s/^/$pattern|/g" $InpFile > $OutFile

When i tried to run the script in the debug mode it was returning the pattern as expected.

Code:
$ >ksh -x hd.ksh head.txt
+ InpFile=head.txt
+ OutFile=/home/my_output.txt
+ head -1 head.txt
+ sed 's/Header\([0-9-]\{10\}\)[0-9-]*/\1/'
+ pattern=2007-12-01
+ sed '/Header/d;s/^/2007-12-01|/g' head.txt
+ 1> /home/my_output.txt

# 6  
Old 08-01-2007
Code:
awk '{ if ( NR == 1 ) { x = substr($0, length("Header") + 1, 10) } else { $0 = x"|"$0; print } }' filename

# 7  
Old 08-01-2007
Hi Lorcan,

I got it with small changes.Header length is fixed always.Thanks.

thats:

#!/bin/ksh
InpFile=$1
OutFile="my_output.txt"
pattern=$(head -1 $1 | awk '{print substr($1,10,10)}');
sed "/Header/d;s/^/$pattern|/g" $InpFile > $OutFile
exit 1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
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. Shell Programming and Scripting

Split file by column value, each with header

Hello all, I have a csv with with different testcase values in column 5. year,min,max,Instrument,Testcase 2016,201,1003,GEOTROPH-02116,TATA7980 2016,53,1011,GEOTROPH-01963,TATA7980 2016,3,1024,GEOTROPH-02067,TATA7980 2016,203,1027,GEOTROPH-02011,TATA7980... (16 Replies)
Discussion started by: senhia83
16 Replies

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

5. UNIX for Dummies Questions & Answers

Column Header in the Spool file

Hi All, I have a problem with the column heading while spooling the data from the database. Since i want the column header, therefore i am not using SET HEADING OFF, and i am getting the header, but for the longer column name, some of the character from the column name is missing. for... (7 Replies)
Discussion started by: Pramod_009
7 Replies

6. Shell Programming and Scripting

Put Header on Text file of all column

Hi I have in put file A.txt ABCDE1 JFHFJFJF3 1 1 SC1 12/10 ABCDE2 JFHFJFJF5 1 1 SC1 12/10 ABCDE3 JFHFJFJF5 1 1 SC1 12/10 ABCDE4 JFHFJFJF6 1 1 SC1 12/10 I want output in .csv with header: Name SUb_N x y No Board ABCDE1 JFHFJFJF3 1 1 SC1 12/10 ABCDE2 JFHFJFJF5 1 1 SC1... (7 Replies)
Discussion started by: pareshkp
7 Replies

7. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

8. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

9. Shell Programming and Scripting

how can I bcp out a table into a text file including the header row in the text file

Hi All, I need to BCP out a table into a text file along with the table headers. Normal BCP out command only bulk copies the data, and not the headers. I am using the following command: bcp database1..table1 out file1.dat -c -t\| -b1000 -A8192 -Uuser -Ppassword -efile.dat.err Regards,... (0 Replies)
Discussion started by: shilpa_acc
0 Replies

10. Shell Programming and Scripting

Convert comma text file to Column in html format

I am trying to generate a report with below file : File1 : EQADM,edrtere9-phys,8122caef0,gpatmon,/bin/ksh,nuten Erick EQADM,edrtere11-phys,8227caef0,gpatmon,/bin/ksh,nuten Erick EQADM,edrtere3-phys,822caef0,gpatmon,/bin/ksh,nuten Erick can you help me convert it to html and add... (9 Replies)
Discussion started by: sriram003
9 Replies
Login or Register to Ask a Question