LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2010, 08:05 AM   #1
Alkass
Member
 
Registered: Mar 2010
Posts: 47

Rep: Reputation: 0
sed with if statement


Hi

I want to read a file, which has something like

5678 with error 4.5(ab)
1234 with error 6.7(fb)

and want to read the lines, and if (ab) is there replace it with something like

5678 10E-6 4.5 10E-6

and respectively , if fb is found

123410E-3 6.710E-3

might be trivial...not for me!

thanks for this!
 
Old 04-25-2010, 08:15 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You can use a construct like this:

sed '/keyword/s/^$/newstuff/' filename

(When keyword is found, replace the entire line with newstuff)

For a really good SED tutorial, go here: http://www.grymoire.com/Unix/Sed.html
 
Old 04-25-2010, 09:07 AM   #3
Alkass
Member
 
Registered: Mar 2010
Posts: 47

Original Poster
Rep: Reputation: 0
the thing is that I do not want to replace the whole line, only the "with error" btwn the numbers, with
10E-6 if (ap) is found after the second number
10E=4 if (fb) is found aftet the second number as well

So, actually, that means two different cases - Is there any way to use if statement in sed?

Thanks
 
Old 04-25-2010, 09:25 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
SED does not have an "if" statement---see the tutorial I linked.

To modify just part of line, you have to be able to uniquely describe it--eg "[0-9]*[^0-9]" means "any number of digits, followed by a non-digit". This still is not quite what you need--because of the decimal point in the 2nd number. For the second case, you can of course just match the (ab) and replace it with the new text.

example:
Code:
sed '/ab/{s/ / 10E-6/; s/(ab)/ 10E-6/}' filename
replaces the first space with a space + 10E-6, then replaces (ab) with a space + 10E-6

For multiple actions in SED, look at the -e flag.

Since your data is ordered in columns, you might be better off with AWK (not my strong suit). That same site has an AWK tutorial.
 
Old 04-28-2010, 02:16 PM   #5
iPenguin
LQ Newbie
 
Registered: Sep 2008
Posts: 12

Rep: Reputation: 2
Hello Alkass,

The sed tool has a method where you can conditionally substitute text. Below is an example script that performs the substitutions. Two methods are shown, one method where the text that is to be processed is stored in variables, which are tested for a match and substituted, and the other method does not use variables. Note the slash at the end of the lines is used for a line continuation character to make this easier to read. Also make note that the characters \| within the sed statement are used to indicate an OR statement for sed. Hope this makes your day.

#!/bin/sh
#
# file : testsubs.sh
#
# Usage : `$ . testsubs.sh'
#


# the error messages set to variables
emsg1='5678 with error 4.5(ab)'
emsg2='1234 with error 6.7(fb)'

# write the error messages to a txt file named tx
echo -e "$emsg1\n$emsg2" > tx

# the args to be matched
argt=' with error'
arg1='(ab)'
arg2='(fb)'

# the substitution tokens
tk1=' 10E-6'
tk2=' 10E-3'

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# display the before and after text
#
echo 'This is the error messages before substitution :'
cat tx

echo -e "\nThis is the error messages after substitution :"
cat tx \
| sed 's/\('"$argt"'\|'"$arg1"'\)/'"$tk1"'/g' \
| sed 's/\('"$argt"'\|'"$arg2"'\)/'"$tk2"'/g'

echo -e "\nThis is another method for substitution :"
cat tx \
| sed 's/\( with error\|(ab)\)/ 10E-6/g' \
| sed 's/\( with error\|(fb)\)/ 10E-3/g'

# EOF #

Last edited by iPenguin; 04-29-2010 at 12:34 PM.
 
Old 04-28-2010, 07:27 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
awk to the rescue

Code:
awk -F"with error" '/ab/{exponent="10E-6"}/fb/{exponent="10E-3"}{gsub(/\(.*/," "exponent);print $1 exponent $2}' file
 
Old 04-29-2010, 01:29 PM   #7
iPenguin
LQ Newbie
 
Registered: Sep 2008
Posts: 12

Rep: Reputation: 2
Hi grail,

Never used it before, but now you have me wanting to dive into awk.

I looked it up on : http://www.grymoire.com/Unix/Awk.html

I see how most of it works, except for the way gsub is performing the substituion.

From the example : {gsub(/\(.*/," "exponent)

From what I understand intuitively, the printed output is filtered through the gsub str subst function. The part I am trying to wrap my brain around is the brackets.

So far, since gsub is a function, I assume gsub() is the function text.

So the next part is to match the content within the slashes : /\(.*/
This grabs all text from the ( character through to the end of the line of text. The bracket is dereferenced as \( so it is interpeted correctly as text and not a function statement bracket.

The comma is the argument separator within the gsub function, then the following text that will be used for substitution is : " "exponent

Where the word exponent is a variable that has been set by the pattern matching in the first part of the awk statement. So the result is that a space character followed by the contents of variable named exponent - that has been set by the conditional substitution in the first part of the awk statement - is printed.

Do I have it right? Thanks, and nice solution.
 
Old 04-30-2010, 12:28 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@iPenguin - spot on

So with the field being omitted the gsub works on the entire line or $0.
So if we take the first line:

5678 with error 4.5(ab)

After the gsub it will become:

5678 with error 4.5 10E-6
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help stripping statement from text file, ksh: sed awk? austin881 Programming 7 07-13-2009 04:31 PM
Help with Sed Statement zcrxsir88 Programming 1 03-17-2009 04:19 PM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
sed parsing into sql statement kcorkran Linux - Newbie 5 02-29-2008 03:04 AM
decoding a sed statement Steve Riley Linux - General 3 01-26-2005 11:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration