2

I have this text file:

some text A=10 some text some more text A more text some other text A=30 other text 

I'm trying to use sed to capture only the numeric value of A. Using this

cat textfile | sed -r 's/.*A=(\S+).*/\1/' 

I get:

10 some more text A more text 30 

But what i really need is:

10 0 30 

If the string A= does not exist output a 0. How can I accomplish this?

1
  • You should accept the most satisfying answer. Commented Nov 22, 2013 at 12:48

8 Answers 8

2

I cannot think on a one-liner, so this is my approach:

while read line do grep -Po '(?<=A=)\d+' <<< "$line" || echo "0" done < file 

I am using the look-behind grep to get any number after A=. In case there is none, the || (else) will print a 0.

Sign up to request clarification or add additional context in comments.

Comments

1

I love code-golf!

sed -e 's/^/A=0 /; s/.*\<A=\(\d\+\).*/\1/' 

This prepends A=0 to the line before substituting.

4 Comments

Ah, I like your solution better. Is the \< thing making sure you get the last 'A='? What does it actually do?
Informally, \< matches the beginning of a word
@cluracan, We always get the last A= because .* is greedy. That's why we prepend A=0 instead of appending.
Oh, cool. I'm always too paranoid to use such fluid rules - but they work!
1

try this one-liner:

awk -F'A=' 'NF==1{print "0";next}{sub(/ .*/,"",$2);print $2}' file 

with your data:

kent$ echo "some text A=10 some text some more text A more text some other text A=30 other text"|awk -F'A=' 'NF==1{print "0";next}{sub(/.*/,"",$2);print $2}' 10 0 30 

Comments

1

awk '{$0=gensub(/^.*A=?([[:digit:]]+).*$/, "\\1", "g"); print($0+0)}' file.txt 

Comments

1

This might work for you (GNU sed):

sed '/.*A=\([0-9][0-9]*\).*/s//\1/;t;s/.*/0/' file 

Look for the string A= followed by one or more numbers and if it occurs replace the whole line by the back reference. Otherwise replace the whole of the line by 0.

2 Comments

+1 this is the only one that is 100% sed, is accurate enough and actually works (and also quite short). b.t.w. what does the ;t; between the two replacements ? (oh, btw, you even can scrap one of the [0-9] from the pattern) :-)
@thom t checks to see if the last substitute command (if any) was a success and if so bails out. As there is no goto place name following the t command it ends the processing of the line. If the substitution was not a success (or no substitute has been done) it replaces the whole line with a 0.
0

I think the best way is to do two different commands - the first replaces lines without 'A=' with the line 'A=0', the second does what you did.

So

cat textfile | sed -r 's/^([^A]|A[^=)*$/A=0/' | sed -r 's/.*A=(\S+).*/\1/' 

Comments

0

How about:

sed -r -e 's/.*A=(\S+).*/\1/' -e 's/.*A.*/0/' 

Comments

0

Some grep-sed-cut combination:

grep -o 'A=\?[0-9]*' input | sed 's/A$/A=0/' | cut -d= -f2 

Produces:

10 0 30 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.