Help with awk/sed word extracting

Help with awk/sed word extracting

Post by Andr » Tue, 23 Sep 2003 07:47:39



Hi
I have a file.txt like this

aword1 aword2 aword3 XXX = aword5 YYYY aword1 aword3 aword3 aword5
aword7 aword2 aword8 aword9 aword10 aword11 aword12 aword13 aword16
aword17

I have to extract 'aword5' using simplest combination of sed/awk. I
believe it can be done with sed only but wasn't able to find exact
"magic" expression.

One expression works though:

sed -n "s/XXX.=./#/p" ./file.txt |awk -F"#" '{ print $2 }' |awk ' {
print $1 }'

I am looking for simpler/faster version... and I don't like the
possibility that # may be already present. Help appreciated!

Thanks
Andrew

 
 
 

Help with awk/sed word extracting

Post by Chris F.A. Johnso » Tue, 23 Sep 2003 08:24:43



> Hi
> I have a file.txt like this

> aword1 aword2 aword3 XXX = aword5 YYYY aword1 aword3 aword3 aword5
> aword7 aword2 aword8 aword9 aword10 aword11 aword12 aword13 aword16
> aword17

> I have to extract 'aword5' using simplest combination of sed/awk. I
> believe it can be done with sed only but wasn't able to find exact
> "magic" expression.

> One expression works though:

> sed -n "s/XXX.=./#/p" ./file.txt |awk -F"#" '{ print $2 }' |awk ' {
> print $1 }'

> I am looking for simpler/faster version... and I don't like the
> possibility that # may be already present. Help appreciated!

    From the code you posted, I believe what you really want is
    "the word following 'XXX = '"; is that right?

    With a POSIX shell, using only cat:

var=`cat file.txt`
var=${var#*XXX = }
echo ${var%% *}

    With bash2, no cat:

var=`< file.txt`
var=${var#*XXX = }
echo ${var%% *}

    With sed:

sed -n "s/.*XXX = \([^ ]*\) .*/\1/p" file.txt

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Help with awk/sed word extracting

Post by j.. » Tue, 23 Sep 2003 08:28:14



> I have a file.txt like this

> aword1 aword2 aword3 XXX = aword5 YYYY aword1 aword3 aword3 aword5
> aword7 aword2 aword8 aword9 aword10 aword11 aword12 aword13 aword16
> aword17

> I have to extract 'aword5' using simplest combination of sed/awk. I
> believe it can be done with sed only but wasn't able to find exact
> "magic" expression.

> One expression works though:

> sed -n "s/XXX.=./#/p" ./file.txt |awk -F"#" '{ print $2 }' |awk ' {
> print $1 }'

> I am looking for simpler/faster version... and I don't like the
> possibility that # may be already present. Help appreciated!

Given the contents you say are in your file, you should be able to do
it without either sed or awk. Try something like this

#!/bin/ksh
f='aword1 aword2 aword3 XXX = aword5 YYYY aword1 aword3 aword3 aword5
aword7 aword2 aword8 aword9 aword10 aword11 aword12 aword13 aword16
aword17'

set -- $f
while [ "$1" != XXX ];do
  shift
done

if [ $# -gt 2 ];then
  shift 2
  echo $1
else
  echo "Not found"
fi

Joe

 
 
 

Help with awk/sed word extracting

Post by laura fairhe » Tue, 23 Sep 2003 15:41:41



Quote:>Hi
>I have a file.txt like this

>aword1 aword2 aword3 XXX = aword5 YYYY aword1 aword3 aword3 aword5
>aword7 aword2 aword8 aword9 aword10 aword11 aword12 aword13 aword16
>aword17

>I have to extract 'aword5' using simplest combination of sed/awk. I
>believe it can be done with sed only but wasn't able to find exact
>"magic" expression.

>One expression works though:

>sed -n "s/XXX.=./#/p" ./file.txt |awk -F"#" '{ print $2 }' |awk ' {
>print $1 }'

>I am looking for simpler/faster version... and I don't like the
>possibility that # may be already present. Help appreciated!

Hi Andrew,

The simplest is probably to use perl;

perl -ne '/(^|\s)XXX\s+=\s+(\S+)/ && print $2;'

I modified your RE to match only when 'XXX' and '=' are seperate
words :)

awk '{for(i=0;++i<NF-1;)if($i=="XXX"&&$(i+1)=="=")print $(i+2)}

This 'sed' script actually uses a marker character '|' like
your hash however at that stage the '|' in the original text
is mapped to a metacharacter ':2',

sed '
  s/:/:1/g
  s/|/:2/g
  s/^XXX[       ]*=[    ]*\([^  ]*\)/|\1|/
  s/[   ]XXX[   ]*=[    ]*\([^  ]*\)/|\1|/
  /|/!d
  s/[^|]*|\([^|]*\).*/\1/
  s/:2/|/g
  s/:1/:/g
  '

byebye
l

Quote:

>Thanks
>Andrew

--

 
 
 

Help with awk/sed word extracting

Post by rakesh shar » Tue, 23 Sep 2003 16:03:09



> Hi
> I have a file.txt like this

> aword1 aword2 aword3 XXX = aword5 YYYY aword1 aword3 aword3 aword5
> aword7 aword2 aword8 aword9 aword10 aword11 aword12 aword13 aword16
> aword17

> I have to extract 'aword5' using simplest combination of sed/awk. I
> believe it can be done with sed only but wasn't able to find exact
> "magic" expression.

> One expression works though:

> sed -n "s/XXX.=./#/p" ./file.txt |awk -F"#" '{ print $2 }' |awk ' {
> print $1 }'

> I am looking for simpler/faster version... and I don't like the
> possibility that # may be already present. Help appreciated!

> Thanks
> Andrew

using 'sed' alone:
sed -e '/=/s/^.* XXX = \([^ ]*\) YYYY .*/\1/' ./file.txt

using 'sed/cut' combo:
sed -e '/=/s/ YYYY / = /' | cut -d= -f2

 
 
 

1. Extracting date fields using sed and awk

My program generates a date output from which I need to extract the
output. It should be done such that year, month, day, hour and minute
fields should be extracted separately.

The date output is in this format.  2006/5/25-9:00. The month, day and
hour fields should have 2 decimals instead of 1.

I tried different ways in sed and awk to extract them but I am not
getting any results. Can anyone let me know how to do this.

Thanks,
doni

2. [Anyone have PPP running wit DIP?]

3. Sed, awk : extract a regexp

4. dbm library from BSD4.3...

5. add blank lines after word MAC (sed/awk or perl??)

6. Problem with Diamond Stealth 64 VRAM / XFree86

7. How to print a word following a pattern in sed, awk or perl

8. Booting from second IDE drive

9. Can sed or awk trim duplicated words?

10. sed/awk - generating Makefiles (Re: awk processing of Makefile macros)

11. Help with extracting word from sentence

12. help with sed extracting email address from text file

13. Char/word/line count with awk. HELP!