Insert output from a file to beginning of line with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert output from a file to beginning of line with sed
# 1  
Old 10-01-2012
Insert output from a file to beginning of line with sed

Hi

I've been trying to search but couldn't quite get the answer I was looking for.

I have a a file that's like this
Code:
Time, 9/1/12
0:00, 1033
0:10, 1044
...
23:50, 1050

How do I make it so the file will be like this?
Code:
9/1/12, 0:00, 1033
9/1/12, 0:10, 1044
...
9/1/12, 23:50, 1050

I tried doing something like this and variations of this, but can't seem to get it to work.
Code:
  for n in {2..144}
   do
     DATETMP=`cat FILE |grep "Time" |awk -F ',' '{print $'$n'}' |sed 's/\//\/\//g'`
    cat FILE |egrep -v "Time" |awk -F ',' '{print$1 "," $'$n'}' |sed -e 's/^/'$DATETMP'/g'
  done

It doesn't seem that $DATETMP is inserting into the beginning of the line.

Any help is extremely appreciated.

Thank you.

Last edited by Franklin52; 10-02-2012 at 04:02 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-01-2012
Code:
awk -F "," 'NR==1{x=$2;next} {sub("^",x",")}1' input_file

This User Gave Thanks to msabhi For This Post:
# 3  
Old 10-01-2012
try..

Code:
 
awk 'NR>1{print "9/1/12,"$0}' filename

# 4  
Old 10-01-2012
Code:
awk 'NR==1{TMP=$2;next}{$0=TMP", "$0}1' infile

# 5  
Old 10-01-2012
Thank you everyone for the help.

I'm one step closer to finishing the script.

---------- Post updated at 11:18 AM ---------- Previous update was at 11:05 AM ----------

Actually sorry, I spoke too soon. I should have explained a little more clear

My source file is more like this
Code:
Time, 9/1/11, 9/2/11 .. 9/30/11
0:00, 1033, 1031, .. 1035
0:10, 1044, 1045, .. 1046
...
23:50, 1050, 1051, .. 1052

How do I make it so the file will be like this?
Code:
9/1/11, 0:00, 1033
9/1/11, 0:10, 1044
...
9/1/11, 23:50, 1050
9/2/11, 0:00, 1031
9/2/11, 0:10, 1045
....
9/2/11, 23:50 1052
..
9/30/11, 00:00, 1050
9/30/11, 00:10, 1045
..
9/30/11, 23:50, 1052

I tried doing something like this,
Code:
i=0
while [ $i -lt ${#TRACK[*]} ]
 do

  for n in {2..144}
   do
    cat $WORKING/${TRACK[i]}.csv |awk -F ',' '{print$1 "," $'$n'}' > tmp00
    awk -F "," 'NR==1{x=$'$n';next} {sub("^",x",")}1' tmp00 
  done

 i=$(( $i + 1 ))
done

I'm getting good results with the first date (sept 1, 11), however once it's sept 2, 11, I get only a blank at the front of the line.
e.g.,
Code:
09/01/11,23:20,1551
09/01/11,23:30,1556
09/01/11,23:40,1506
09/01/11,23:50,1480
,0:00,1455
,0:10,1401
,0:20,1382
,0:30,1309

How do I get the $n variable into awk -F "," 'NR==1{x=$'$n';next} {sub("^",x",")}1' properly?

Any help is appreciated.

Thank you

---------- Post updated at 11:49 AM ---------- Previous update was at 11:18 AM ----------

Nevermind, I figured it out.

Thank you all for your help.

Last edited by Franklin52; 10-02-2012 at 04:04 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use sed to insert character in the beginning of file path?

I need to manipulate one Database file on Solaris 11 in which contains more than 5000 lines of data file path like this: '/data1/oradata/DBNAME/system01.dbf', '/data7/oradata/DBNAME/undotbs1_01.dbf', '/data1/oradata/DBNAME/sysaux01.dbf', '/data28/oradata/DBNAME/userdata01.dbf', ... (6 Replies)
Discussion started by: duke0001
6 Replies

2. UNIX for Beginners Questions & Answers

Insert the line number from text file to filename output

Hi everyone :) I have a file "words.txt" containing hundreds of lines of text. Each line contains a slogan. Using the code below i am able to generate an image with the slogan text from each line. The image filename is saved matching the last word on each line. Example: Line 1: We do... (2 Replies)
Discussion started by: martinsmith
2 Replies

3. Shell Programming and Scripting

Insert text at the beginning of every even number line

i am trying to insert text at the beginning of every even number line with awk i can do it with odd number lines with this command awk 'NR%2{$0="some text "$0}1' filehow can i edit this command thanks (5 Replies)
Discussion started by: bob123
5 Replies

4. UNIX for Dummies Questions & Answers

sed command to Insert a line before the last four lines of the file

By using sed command, How to insert a new line before the last four lines of the file. Old Line Old Line NEW LINE! Old Line Old Line Old Line Old Line (8 Replies)
Discussion started by: wridler
8 Replies

5. Shell Programming and Scripting

sed insert new line does not update file

Hi all, I have a file called "text.cpp" with the first line of "1" afterwards I tried in Ubuntu to type the following sed '12iasdasdasdasdsad' test.cpp > output.txt however when I tried to see the result of output.txt #cat output.txt 1 why is the line 12 is not updated to the... (6 Replies)
Discussion started by: peuceul
6 Replies

6. Shell Programming and Scripting

SED - insert space at the beginning of line and multi replace command

hi I am trying to use SED to replace the line matching a pattern using the command sed 'pattern c\ new line ' <file1 >file 2 I got two questions 1. how do I insert a blank space at the beginning of new line? 2. how do I use this command to execute multiple command using the -e... (5 Replies)
Discussion started by: piynik
5 Replies

7. Shell Programming and Scripting

Insert shell command into first line of output file

How can I insert the command executed on the shell into the first line of my output file? For example if I execute; zcat *.gz |grep “User5501” > users.out How can I make my users.out look like; zcat *.gz |grep “User5501” > users.out User5501 PA User5501 UA User5501 ZA... (3 Replies)
Discussion started by: lewk
3 Replies

8. Shell Programming and Scripting

Using sed to insert text file at first line

sed '1r file.txt' <source.txt >desti.txt This example will insert 'file.txt' between line 1 and 2 of source.txt. sed '0r file.txt' <source.txt >desti.txt gives an error message. Does anyone know how 'sed' can insert 'file.txt' before the first line of source.txt? (18 Replies)
Discussion started by: psve
18 Replies

9. Shell Programming and Scripting

Insert output into file at line number

I need to insert the output of a script into a specific line number of a txt file. I've read the Sed man page and searched the forums and it's not immediately clear how I would go about doing this. (4 Replies)
Discussion started by: pluto7777
4 Replies

10. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies
Login or Register to Ask a Question