Extract date from file header and prefix it to all lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract date from file header and prefix it to all lines
# 1  
Old 10-11-2009
Question Extract date from file header and prefix it to all lines

Hello All,
I have a file in the following format. I want to extract the date(020090930, 020090929) in the string "STPAGE020090930" and "STPAGE020090929" and prefix it to all lines below them. The output must be put into a new file.

Code:
  STPAGE020090930
xyzz aalc 1+0000000000412600000000090000000000213320000000000000000000000372000000002732832000000000009524
xyzz aalc 200000000000000000000000000000000000000000000000018000000000267475000000000000947
xyzz aalc 3000003579700000000116247300000000000000000000000000000000000000000000000000000000000000000000000000007355
xyzz aclp 1+0000000000080800000000000000000000000000000000000000000000000009000000000044886000000000000315
xyzz aclp 200000000040000000000337500000000000010950000000000000000000000000000000000000000
xyzz aclp 3000000061200000000127983100000000000000000000000000000000000000010000001000000000000017460000000000001460
xyzz aclp 4000001000000
xyzz aclp 568618d102       ogsm          200909240000010000000000000003179bc000000.320000slhq l02 lhq000019         lhq000108
00000000008
  STPAGE020090929
xyzz wstn 1+0000000011820000000000000000000000000000000000000000000000000001000000000003909000000000000084
xyzz wstn 3000000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
xyzz wyom 1+0000000000391900000000000000000000000000000000000000000000000000000000000000000000000000000000
xyzz wyom 3000000038100000000011304900000000000000000000000000098800000000000000000000000000000000000000000000000070
00000000004

The desired output is as shown below:
Code:
  STPAGE020090930
020090930 XYZZ AALC 1+0000000000412600000000090000000000213320000000000000000000000372000000002732832000000000009524
020090930 XYZZ AALC 200000000000000000000000000000000000000000000000018000000000267475000000000000947
020090930 XYZZ AALC 3000003579700000000116247300000000000000000000000000000000000000000000000000000000000000000000000000007355
020090930 XYZZ ACLP 1+0000000000080800000000000000000000000000000000000000000000000009000000000044886000000000000315
020090930 XYZZ ACLP 200000000040000000000337500000000000010950000000000000000000000000000000000000000
020090930 XYZZ ACLP 3000000061200000000127983100000000000000000000000000000000000000010000001000000000000017460000000000001460
020090930 XYZZ ACLP 4000001000000
020090930 XYZZ ACLP 568618D102       OGSM          200909240000010000000000000003179BC000000.320000SLHQ L02 LHQ000019         LHQ000108
020090930 00000000008
  STPAGE020090929
020090929 XYZZ WSTN 1+0000000011820000000000000000000000000000000000000000000000000001000000000003909000000000000084
020090929 XYZZ WSTN 3000000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
020090929 XYZZ WYOM 1+0000000000391900000000000000000000000000000000000000000000000000000000000000000000000000000000
020090929 XYZZ WYOM 3000000038100000000011304900000000000000000000000000098800000000000000000000000000000000000000000000000070
020090929 00000000004

Any help on the above will be appreciated.
Thank you,
John

Last edited by john2022; 10-11-2009 at 05:01 AM.. Reason: Changed Title
# 2  
Old 10-11-2009
Code:
cat t.pl

while(<>)  {
    if ($_ =~ /STPAGE(.*)/)  {
        $date = $1;
        print $_;
        next;
    }
    print "$date $_";
}

Code:
$ perl t.pl input-file

STPAGE020090930
020090930 xyzz aalc 1+0000000000412600000000090000000000213320000000000000000000000372000000002732832000000000009524
020090930 xyzz aalc 200000000000000000000000000000000000000000000000018000000000267475000000000000947
020090930 xyzz aalc 3000003579700000000116247300000000000000000000000000000000000000000000000000000000000000000000000000007355
020090930 xyzz aclp 1+0000000000080800000000000000000000000000000000000000000000000009000000000044886000000000000315
020090930 xyzz aclp 200000000040000000000337500000000000010950000000000000000000000000000000000000000
020090930 xyzz aclp 3000000061200000000127983100000000000000000000000000000000000000010000001000000000000017460000000000001460
020090930 xyzz aclp 4000001000000
020090930 xyzz aclp 568618d102       ogsm          200909240000010000000000000003179bc000000.320000slhq l02 lhq000019         lhq000108
020090930 00000000008
  STPAGE020090929
020090929 xyzz wstn 1+0000000011820000000000000000000000000000000000000000000000000001000000000003909000000000000084
020090929 xyzz wstn 3000000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
020090929 xyzz wyom 1+0000000000391900000000000000000000000000000000000000000000000000000000000000000000000000000000
020090929 xyzz wyom 3000000038100000000011304900000000000000000000000000098800000000000000000000000000000000000000000000000070
020090929 00000000004

It is always good to explain, what you tried and the difficulty you are facing, instead of just asking for a solution ?! Isn't it ?!
# 3  
Old 10-11-2009
Code:
awk '
  /STPAGE/ { DATE = substr( $1, length($1)-8 ); print; next }
  { print DATE, $0 }
' file1
 
  STPAGE020090930
020090930 xyzz aalc 1+0000000000412600000000090000000000213320000000000000000000000372000000002732832000000000009524
020090930 xyzz aalc 200000000000000000000000000000000000000000000000018000000000267475000000000000947
020090930 xyzz aalc 3000003579700000000116247300000000000000000000000000000000000000000000000000000000000000000000000000007355
020090930 xyzz aclp 1+0000000000080800000000000000000000000000000000000000000000000009000000000044886000000000000315
020090930 xyzz aclp 200000000040000000000337500000000000010950000000000000000000000000000000000000000
020090930 xyzz aclp 3000000061200000000127983100000000000000000000000000000000000000010000001000000000000017460000000000001460
020090930 xyzz aclp 4000001000000
020090930 xyzz aclp 568618d102       ogsm          200909240000010000000000000003179bc000000.320000slhq l02 lhq000019         lhq000108
020090930 00000000008
  STPAGE020090929
020090929 xyzz wstn 1+0000000011820000000000000000000000000000000000000000000000000001000000000003909000000000000084
020090929 xyzz wstn 3000000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
020090929 xyzz wyom 1+0000000000391900000000000000000000000000000000000000000000000000000000000000000000000000000000
020090929 xyzz wyom 3000000038100000000011304900000000000000000000000000098800000000000000000000000000000000000000000000000070
020090929 00000000004

# 4  
Old 10-11-2009
I have a different opinion, maybe that's what the OP want Smilie
Code:
 awk '/STPAGE/{DATE=substr($1, length($1)-7)}{printf ((!/[a-zA-Z]/)?$0:((/[a-z]/)?RS DATE FS $0:((/[A-Z]/ && NR!=1)?RS $0:$0)))}' file
  STPAGE020090930
20090930 xyzz aalc 1+0000000000412600000000090000000000213320000000000000000000000372000000002732832000000000009524
20090930 xyzz aalc 200000000000000000000000000000000000000000000000018000000000267475000000000000947
20090930 xyzz aalc 3000003579700000000116247300000000000000000000000000000000000000000000000000000000000000000000000000007355
20090930 xyzz aclp 1+0000000000080800000000000000000000000000000000000000000000000009000000000044886000000000000315
20090930 xyzz aclp 200000000040000000000337500000000000010950000000000000000000000000000000000000000
20090930 xyzz aclp 3000000061200000000127983100000000000000000000000000000000000000010000001000000000000017460000000000001460
20090930 xyzz aclp 4000001000000
20090930 xyzz aclp 568618d102       ogsm          200909240000010000000000000003179bc000000.320000slhq l02 lhq000019         lhq00010800000000008
  STPAGE020090929
20090929 xyzz wstn 1+0000000011820000000000000000000000000000000000000000000000000001000000000003909000000000000084
20090929 xyzz wstn 3000000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
20090929 xyzz wyom 1+0000000000391900000000000000000000000000000000000000000000000000000000000000000000000000000000
20090929 xyzz wyom 300000003810000000001130490000000000000000000000000009880000000000000000000000000000000000000000000000007000000000004

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

Bash to extract file prefix and from input to use in output

In the bash below which does execute I am trying to extract the contents of ${id} is 1234, as ${id} stores the variable that changes each time. After the path is removed the contents of ${id} are stored in pref, so they can be used in the output. Currently I am not able to extract the 1234 in the... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

4. Shell Programming and Scripting

Insert date/time header at top of file

I'm trying to take mrt output and put it at the top of a file along with the date and time. I was able to do it at the bottom of the file with the following printf "********** $(date) **********\n\n" >> $OUTPUT_PATH/$HOSTNAME mtr -r -w -c 10 $HOSTADDRESS >> $OUTPUT_PATH/$HOSTNAME printf... (2 Replies)
Discussion started by: kramer65
2 Replies

5. Shell Programming and Scripting

Prefix a variable in the first column of all the records of the files with and without header

In a bash shell, I have to prefix a variable to two .CSV files File1.CSV and File2.CSV. One of the files has a header and the other one is with no header in the below format: "value11","value12","value13","value14","value15","value16" "value21","value22","value23","value24","value25","value26"... (7 Replies)
Discussion started by: dhruuv369
7 Replies

6. Shell Programming and Scripting

Sum from successive lines following date header to create data for graphing connections

Hi, I have a log file created from a load balancer showing connections to each member of a two member pool with the following format (where first field is source IP, second field is load balanced IP address and third field is destination member. I need to plot a graph by date/time and number of... (5 Replies)
Discussion started by: shog63
5 Replies

7. Shell Programming and Scripting

Using AWK BEGIN to extract file header info into variables

Hi Folks, I've searched for this for quite a while, but can't find any solution - hope someone can help. I have various files with standard headers. eg. <HEADER> IP: 1.2.3.4 Username: Joe Time: 12:00:00 Date: 23/05/2010 </HEADER> This is a test and this part can be any size... (6 Replies)
Discussion started by: damoske
6 Replies

8. Shell Programming and Scripting

Trying to change header date of a file

Hi, I am trying to write a code for changing header date of a file with a date of 6 month back. Below is the header date on which I am working - 20080531 I want to change it as 20071130 Please suggest me in the way like ,I can change any date in a file with date of 6 months back. ... (9 Replies)
Discussion started by: Monalisa
9 Replies

9. Shell Programming and Scripting

how to extract paragraphs from file in BASH script followed by prefix ! , !! and !!!

I]hi all i am in confusion since last 2 days :( i posted thraed yesterday and some friends did help but still i couldnt get solution to my problem let it be very clear i have a long log file of alkatel switch and i have to seperate the minor major and critical alarms shown by ! , !! and !!!... (6 Replies)
Discussion started by: nabmufti
6 Replies

10. Shell Programming and Scripting

Total of lines w/out header and footer incude for a file

I am trying to get a total number of tapes w/out headers or footers in a ERV file and append it to the file. For some reason I cannot get it to work. Any ideas? #!/bin/sh dat=`date +"%b%d_%Y"` + date +%b%d_%Y dat=Nov16_2006 tapemgr="/export/home/legato/tapemgr/rpts"... (1 Reply)
Discussion started by: gzs553
1 Replies
Login or Register to Ask a Question