sed replace string in file with stdin


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replace string in file with stdin
# 1  
Old 05-15-2011
sed replace string in file with stdin

Hi
Im trying to do the following:

Code:
grep -H  Date: out/* | sed  's/':'/ /' | awk '$4 ~ /^[Aa-Zz]/ {print $1}' | while read VARIABLE; do
awk '{print $1,$3,$2}' $VARIABLE | sed (take stdin and replace a string in $VARIABLE)
done

What this is basically doing is finding all files with Date: in them where the 4th column starts with a letter, then passes out the file name to awk which re-arranges certain columns within the file.

What I need to do then is pipe the output of awk to sed where it replaces a string with the output of awk.

I cant find out if sed allows you to define the replacement sting as stdin.

Thanks
# 2  
Old 05-15-2011
you could use a read command to take input from stdin and use that variable in your sed command.

If you need to read stdin for every execution of while, then put the read command insider loop. as the awk and sed is also a part of loop, this should work.
# 3  
Old 05-15-2011
I feel something is wrong with the logic?
Output of awk inside the while will give you more than one value(of course it depends on the file) and which value will you chose to replace using sed and with what?

regards,
Ahamed
# 4  
Old 05-15-2011
Quote:
Originally Posted by ahamed101
I feel something is wrong with the logic?
Output of awk inside the while will give you more than one value(of course it depends on the file) and which value will you chose to replace using sed and with what?

regards,
Ahamed
Ok yeh i just realised that this wont work, I was testing with files that only had Date: blah blah in them but normally they will have other data as well which will stil get caught up. Thanks for the heads up.

Any idea of a better way to do this?
The whole goal of this is that I have a line in multiple files with:

Date: Thu, Aug 23 2001 21:27:04 -0400

As an example. What im trying to do is determine if a file has a date format of month day year and then swap them around so it would be instead:

Date: Thu, 23 Aug 2001 21:27:04 -0400

So just simply swapping the two columns around for files that have the wrong date format.
# 5  
Old 05-15-2011
Try this

Code:
sed 's/\(Date:.*, \)\([Aa-Zz]..\) \([0-9].\)\(.*\)/\1\3 \2\4/g' inputfile

regards,
Ahamed
This User Gave Thanks to ahamed101 For This Post:
# 6  
Old 05-15-2011
thanks, that is much simpler and works perfectly. thank you

Ive ben looking around for something else to deal with the same line of data.

Sometimes the data will be formatted like so:

Date: Thu, Aug 23 2001 09:27 PM -0400

Is it possible to calculate values in sed, so say is column 7 Starts with P then add 12 to column 6 to make it 24 hr format so 21:27:00
# 7  
Old 05-15-2011
Try this for your second scenario

Code:
awk '/PM/{split($6,_1,":");$6=_1[1]+12":"_1[2]}/AM|PM/{$6=$6":00";$7=$8;NF=7}1' infile

regards,
Ahamed
This User Gave Thanks to ahamed101 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

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

sed - Replace string with file contents

Hello, I have two files: file1 and file2 file1 has the following info: --- host: "localhost" port: 3000 reporter_type: "zookeeper" zk_hosts: - "localhost:2181" file2 contains an IP address (1.1.1.1) What I want to do is replace localhost with 1.1.1.1, so that the... (4 Replies)
Discussion started by: Jay Kah
4 Replies

4. Shell Programming and Scripting

How to replace a string with a variable in a file using sed?

I have a file having some text like: PATH_ABC=/user/myLocation I have to replace "/user/myLocation" with a session variable say, $REPLACE_PATH, where $REPLACE_PATH=/user/myReplaceLocation The following sed command is not working. It is writing PATH_ABC=$REPLACE_PATH in the file ... (2 Replies)
Discussion started by: SKhan
2 Replies

5. Shell Programming and Scripting

sed or awk to replace a value in a certain line from another file containing a string

Hi experts, In my text file I have the following alot of lines like below. input.k is as follows. 2684717 -194.7050476 64.2345581 150.6500092 0 0 2684718 -213.1575623 62.7032242 150.6500092 0 0 *INCLUDE $# filename... (3 Replies)
Discussion started by: hamnsan
3 Replies

6. Shell Programming and Scripting

How to replace a string containing @ in a file using sed ?

I have a list of names and email addresses. Sample File - username=poga--poga@yahoo.com new-york,US 512834 username=poga123--poga123@hotmail.com new-jersey,US 0894753 Requirement is to replace the email ids as dummy_username@xyz.com using sed only. Output File -... (4 Replies)
Discussion started by: poga
4 Replies

7. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

8. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

9. Shell Programming and Scripting

How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file? (7 Replies)
Discussion started by: gomes1333
7 Replies

10. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies
Login or Register to Ask a Question