SED for text file modification


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED for text file modification
# 1  
Old 07-07-2006
Error SED for text file modification

Hi all,

i have a text file something like that

FLAG DATE(YYYYMMDD) TIME(HHMMSS)

FLAG1 DATE20060101 141216
FLAG1 DATE20070101 141216
FLAG2 DATE20060102 140010
FLAG2 DATE20060103 101212
FLAG1 DATE20070101 101111


using sed, i want to change some of these dates depending on their flag

for example i want to change all FLAG1 dates to a constant date
like DATE20081201
how can i achieve this?
(i am really weak on regular expressions)
# 2  
Old 07-07-2006
Code:
sed -e "s_FLAG1_DATE20081201_g" in > out

# 3  
Old 07-07-2006
Error

thanks for the reply,
but when i run sed as you described i got this output

DATE20081201 DATE20060101 141216
DATE20081201 DATE20070101 141216
FLAG2 DATE20060102 140010
FLAG2 DATE20060103 101212
DATE20081201 DATE20070101 101111

that is not what i want,
i want the out like the one below:

FLAG DATE(YYYYMMDD) TIME(HHMMSS)

FLAG1 DATE20081201 141216
FLAG1 DATE20081201 141216
FLAG2 DATE20060102 140010
FLAG2 DATE20060103 101212
FLAG1 DATE20081201 101111


and what is that syntax : "s_FLAG1_DATE20081201_g"
can u explain a little bit?
# 4  
Old 07-07-2006
try this,

Code:
awk -F" " '{ if ( $1 !~ /FLAG1/ ) { print } else { print $1" ""DATE20081201"" "$3 } }' inputfile

# 5  
Old 07-07-2006
thank you very much matrixmadhan for this awk answer,
a little bit explanation about your usage of awk in that line will
be very appreciated.
i am confused about that braces and double quotation marks.
# 6  
Old 07-07-2006
Quote:
thank you very much matrixmadhan for this awk answer,
you are welcome
Quote:
i am confused about that braces and double quotation marks.
break it .... everything would become simpler
Quote:
a little bit explanation about your usage of awk in that line will
be very appreciated.
here it is
Code:
awk -F" " '{ if ( $1 !~ /FLAG1/ ) { print } else { print $1" ""DATE20081201"" "$3 } }' inputfile

Delimiter between fields is space

check for the first field in the line if it is not FLAG1 we can print everything as it is - just use print
if the first field in the line is FLAG1 then just print the first field (FLAG1), followed by a space denoted by " " and the constant date field and space again " " and the third field as such..
# 7  
Old 07-07-2006
everything is really very clear now,
thank you again matrixmadhan
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to remove text from file

Trying to use sed to, in-place, remove specific text from a file. Since there are / in the text I use | to escape that character. Thank you :). sed -i -e 's|xxxx://www.xxx.com/xx/xx/xxx/.*/|' /home/cmccabe/list sed: -e expression #1, char 51: unterminated `s' command (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Help with text modification and displaying

I have a file storing some text and another file storing some numbers I want to display characters other than the specified place of strings one.txt xyz abc 233 skfo 4r443 sfs abc abcd sd fsdf sdfd abc 11 abc 33 abc dsaf two.txt Nt_djd_k='5-6,7-9' Nt_hh_l='3-6,7-8' a=`grep... (4 Replies)
Discussion started by: rahulsk
4 Replies

3. Shell Programming and Scripting

Modify text file using sed

Hello all, I have some text files I need to do the following on: Delete banner page (lines 1-56) --I am doing this using sed Remove ^M --I am doing this using vi Remove trailer page --this can vary based on the contents of the file, it usually starts with *************************** I am... (5 Replies)
Discussion started by: jeffs42885
5 Replies

4. Shell Programming and Scripting

IP Address Modification through awk/sed

Hi, I have to modify the 2nd and 3rd octet of the IP address through awk/sed. For Example: Given IP is : 10.205.22.254, it should be modified as 10.105.100.254 through awk/sed. Kindly help me on this and let me know if you have any questions. Thanks in advances. (2 Replies)
Discussion started by: kumarbka
2 Replies

5. Shell Programming and Scripting

Help with sed and inserting text from another file

I need to insert text from one file into another file after specific term. I guess sed is the best method of doing this and I can insert a specified text string using this script but I am not sure how to modify it to insert text from another file: #!/bin/sh sed 's/\<VirtualHost... (17 Replies)
Discussion started by: barrydocks
17 Replies

6. UNIX for Dummies Questions & Answers

Using sed to replace / in text file

Hi, I want to use sed to replace " /// " with "///" in a text file. However I am getting error messages when I use sed 's/ /// /////g' input.txt > output.txt. How do I go about doing this in sed? Input: 219518_s_at 0.000189 ELL3 / SERINC4 Output: 219518_s_at 0.000189 ELL3/SERINC4 (5 Replies)
Discussion started by: evelibertine
5 Replies

7. Shell Programming and Scripting

Text string modification

My shell needs to take a 10 digit number and output it in the forum nnn-nnn-nnnn I considered using the fold command but that won't help too much. (11 Replies)
Discussion started by: computethis
11 Replies

8. HP-UX

Pid X killed due to text modification or page I/O error

Hello everybody, I have a HP-UX B.11.11. I had one disk and added one new. When trying to configure the second disk Not Using the Logical Volume Manager(from SAM) I have this error: Pid X killed due to text modification or page I/O error I tryed to add another partion on the first disk,... (5 Replies)
Discussion started by: savus
5 Replies

9. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

10. Programming

Text Modification and page I/O error

Hi!, while launching my C application on HP-UX 10.2 workstation, I get the following error message sometime: Pid 9951 killed due to text modification or page I/O error Could someone tell me what does this error means?? and how is this caused? Thanx in Advance, JP (4 Replies)
Discussion started by: jyotipg
4 Replies
Login or Register to Ask a Question