Read line from the file and append it to each row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read line from the file and append it to each row
# 1  
Old 05-24-2014
Read line from the file and append it to each row

Hi All,

We have a file in the following format:

Code:
 0.010000  $  ITI   11 LV2         $    40456211  $
 0.135000  $  ITI   11 LV1         $    40512211  $
 1.215600  $  ITI   11 ITI3         $    41406211  $
 24/05/2014 14:05:02
  
 0.030000  $  ITI   11 LV2       $    40456211  $
 0.115000  $  ITI   11 LV1       $    40512211  $
 1.265600  $  ITI   11 ITI3       $    41406211  $
 24/05/2014 14:10:02
 0.000000  $  ITI   11 LV2         $    40456211  $
 0.145000  $  ITI   11 LV1         $    40512211  $
 1.215600  $  ITI   11 ITI3         $    41406211  $
  24/05/2014 14:15:02

We want to read the time given in some rows and prepend them as first column in each row preceding it. We will also remove empty/blank rows. So it will look something like this.

Code:
24/05/2014 14:05:02 $  0.010000  $  ITI   11 LV2       $    40456211  $
  24/05/2014 14:05:02 $  0.135000  $  ITI   11 LV1       $    40512211  $
  24/05/2014 14:05:02 $  1.215600  $  ITI   11 ITI3       $    41406211  $
  24/05/2014 14:10:02 $  0.030000  $  ITI   11 LV2       $    40456211  $
  24/05/2014 14:10:02 $  0.115000  $  ITI   11 LV1       $    40512211  $
  24/05/2014 14:10:02 $  1.265600  $  ITI   11 ITI3       $    41406211  $
  24/05/2014 14:15:02 $  0.000000  $  ITI   11 LV2       $    40456211  $
  24/05/2014 14:15:02 $  0.145000  $  ITI   11 LV1       $    40512211  $
  24/05/2014 14:15:02 $  1.215600  $  ITI   11 ITI3       $    41406211  $

Thanks for your help.
# 2  
Old 05-24-2014
Code:
$ awk 'NF==2{$1=$1;gsub(/\*/,$0 c,s);print s;s="";next}NF{$1=$1;s = s ? s ORS "*"$0 : "*"$0 }' c=' $ ' file

24/05/2014 14:05:02 $ 0.010000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:05:02 $ 0.135000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:05:02 $ 1.215600 $ ITI 11 ITI3 $ 41406211 $
24/05/2014 14:10:02 $ 0.030000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:10:02 $ 0.115000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:10:02 $ 1.265600 $ ITI 11 ITI3 $ 41406211 $
24/05/2014 14:15:02 $ 0.000000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:15:02 $ 0.145000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:15:02 $ 1.215600 $ ITI 11 ITI3 $ 41406211 $

This keeps your original formatting
Code:
$ awk 'NF==2{gsub(/\*/,$0 c,s);print s;s="";next}NF{s = s ? s ORS "*"$0 : "*"$0 }' c=' $ ' file

---------- Post updated at 05:45 PM ---------- Previous update was at 05:40 PM ----------

OR

Code:
$ awk 'NF==2{d=$0;next}NF{print d,"$",$0}' <(tac file) | tac

# 3  
Old 05-24-2014
Quote:
Originally Posted by Akshay Hegde
Code:
$ awk 'NF==2{$1=$1;gsub(/\*/,$0 c,s);print s;s="";next}NF{$1=$1;s = s ? s ORS "*"$0 : "*"$0 }' c=' $ ' file

24/05/2014 14:05:02 $ 0.010000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:05:02 $ 0.135000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:05:02 $ 1.215600 $ ITI 11 ITI3 $ 41406211 $
24/05/2014 14:10:02 $ 0.030000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:10:02 $ 0.115000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:10:02 $ 1.265600 $ ITI 11 ITI3 $ 41406211 $
24/05/2014 14:15:02 $ 0.000000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:15:02 $ 0.145000 $ ITI 11 LV1 $ 40512211 $
24/05/2014 14:15:02 $ 1.215600 $ ITI 11 ITI3 $ 41406211 $

This keeps your original formatting
Code:
$ awk 'NF==2{gsub(/\*/,$0 c,s);print s;s="";next}NF{s = s ? s ORS "*"$0 : "*"$0 }' c=' $ ' file

---------- Post updated at 05:45 PM ---------- Previous update was at 05:40 PM ----------

OR

Code:
$ awk 'NF==2{d=$0;next}NF{print d,"$",$0}' <(tac file) | tac

could you explain that:
Code:
gsub(/\*/,$0 c,s)

who's "s" and "c" ???
# 4  
Old 05-24-2014
Quote:
Originally Posted by protocomm
could you explain that:
Code:
gsub(/\*/,$0 c,s)

who's "s" and "c" ???
Hi protocomm,

1. Variable s saves every line $0 where NF is greater than 0 NF{.., with ORS,so blank line are skipped here.

2. I used *, inorder to replace it with coming date and time where number of fields equal to 2 NF==2.

3. Once number of fields NF equal to 2, substitute $0 along with variable c for all * in variable s. Global subsitution using gsub function.

Just see difference when variable c is set to ' $ '

Code:
$ awk 'NF==2{$1=$1;gsub(/\*/,$0 c,s);print s;s="";next}NF{$1=$1;s = s ? s ORS "*"$0 : "*"$0 }' c=' $ ' file
24/05/2014 14:05:02 $ 0.010000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:05:02 $ 0.135000 $ ITI 11 LV1 $ 40512211 $
....

Here variable c is empty

Code:
$ awk 'NF==2{$1=$1;gsub(/\*/,$0 c,s);print s;s="";next}NF{$1=$1;s = s ? s ORS "*"$0 : "*"$0 }' c='' file
24/05/2014 14:05:020.010000 $ ITI 11 LV2 $ 40456211 $
24/05/2014 14:05:020.135000 $ ITI 11 LV1 $ 40512211 $
....


Last edited by Akshay Hegde; 05-26-2014 at 09:51 AM.. Reason: grammar or typo
These 2 Users Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read row number from 1 file and print that row of second file

Hi. How can I read row number from one file and print that corresponding record present at that row in another file. eg file1 1 3 5 7 9 file2 11111 22222 33333 44444 55555 66666 77777 88888 99999 (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

2. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

3. UNIX for Dummies Questions & Answers

append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script. For example if I have Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies

4. Shell Programming and Scripting

Append data to new row in CSV file every day

Hi All I will run the same script every day in corn and output should go to same CSV file but in different row with dates on it. Below is my example in attached format. Script i am using to collect switch port online DATE=`date '+%d-%m-%y'` for f in `cat... (1 Reply)
Discussion started by: ranjancom2000
1 Replies

5. Shell Programming and Scripting

Get a group of line from different file and put them into one file row by row

hi guys, today i'm stuck in a new problem. the title explain more or less but a particular had been omitted. So i'm going to describe below the situation with an example. I have different html files and each of them have a consecutive lines group inside that i want to extract. example: ... (8 Replies)
Discussion started by: sbobotex
8 Replies

6. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

7. Shell Programming and Scripting

Append Second Row to First Row @ end in a file

Hi I want to append line 2n to 2n-1 line where n=1...LastLine in my file. eg: Actual File: Hello Everyone Welcome TO Unix I need Your help Required File: HelloEveryone WelcomeTO Unix I needYour help (5 Replies)
Discussion started by: dashing201
5 Replies

8. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies

9. Shell Programming and Scripting

read from a specific pattern from one file and append it to another

Hi! Everyone, Say this file1 -------------- line 1 51610183 420001010 0010CTCTLEDPPOO 2151610183 line 2 2151610183 420001010 0030A2TH2 line 3 2151610183 420001010 0040A2TH3 line 4 2151610183 420001010 ... (0 Replies)
Discussion started by: kinkar_ghosh
0 Replies

10. UNIX for Dummies Questions & Answers

How to read and write a random row from a file?

Lets say I have a file abc.txt and it has about 35 million rows. I would like to take a sample of 100 random rows from that file for my testing purpose and write it to a file say test.txt. How do I do this operation? Thanks, Sashank (9 Replies)
Discussion started by: sashankkrk
9 Replies
Login or Register to Ask a Question