Results 1 to 8 of 8
I´m trying to find a line and the following one with a pattern i´ve got in a variable...
i´ve tried with sed and awk, see for example the following code ...
- 12-27-2008 #1Just Joined!
- Join Date
- Dec 2008
- Posts
- 4
can SED/AWK take variable as Pattern?
I´m trying to find a line and the following one with a pattern i´ve got in a variable...
i´ve tried with sed and awk, see for example the following code would return the line i want and the next one...
sed -n '/phone/,+1 p' file.txt
i would need something like ---- sed -n '/$myvariable/,+1 p' file.txt ----- but the problem is because of the inverted commas ' ' i cannot use $ character to indicate i want to search for the value of the variable...
how could i do it?? i would really really appreciate your help! i´m kindda desperate!!!
Many thanks to you all!
- 12-27-2008 #2
Try changing the single quotes to double quotes. That should work.
e.g. sed -n "$myvariable p" file.txt \
- 12-29-2008 #3Just Joined!
- Join Date
- Dec 2008
- Posts
- 4
I've been trying to change for double quotes in different ways, but that's all I get:
: sed -n "$fulltime p" $filename
No addresses allowed: 08:04:22.712 p
any suggestions?
- 12-29-2008 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
An illustration:
Producing:Code:#!/bin/bash - # @(#) s1 Demonstrate double quotes around variable, bash, sed. echo echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) sed set -o nounset echo FILE=${1-data1} cat > $FILE <<EOF first line second line a phone number is 555-1234 next line last line EOF echo " Data file $FILE:" cat $FILE echo echo " Results:" myvariable="phone" sed -n "/$myvariable/,+1 p" $FILE exit 0
Experiment and see man pages for details ... cheers, drlCode:% ./s1 (Versions displayed with local utility "version") Linux 2.6.11-x1 GNU bash 2.05b.0 sed GNU sed version 4.1.2 Data file data1: first line second line a phone number is 555-1234 next line last line Results: a phone number is 555-1234 next line
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 12-29-2008 #5Just Joined!
- Join Date
- Dec 2008
- Posts
- 4
hi dlr and cheapscotchron,
i'm still getting:
sed: command garbled: /08:04:22.712/,+1 p
i think it's because i'm using a SunOS 5.10 that doesn't have the GNU included...
- 12-29-2008 #6
Should work under solaris.
Can you post your entire script? Are you setting the shell properly?
e.g. Notice the first line of drl's script...
#!/bin/bash
This invokes the borne (again) shell.
FYI.. this should also work under kshell (ksh). Syntax would be slightly different for cshell (csh).
- 12-29-2008 #7Just Joined!
- Join Date
- Dec 2008
- Posts
- 4
It could be a matter of invoking... as I'm not sure what to do at all!
Guys, many thanks for all, but I managed to do the same without using awk/sed as it's been a nightmare... If you're interested you've got it here:
tail +$((`grep -n $fulltime $filename | cut -d: -f1`)) $filename | head -n 2
that takes the patter $fulltime and searches it in $filename and then returns the first match of that and the following line...
I really appreciate your help! Thank you!
- 12-29-2008 #8Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Producing:Code:#!/bin/bash - # @(#) s1 Demonstrate double quotes around variable, bash, sed. echo echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) sed nawk set -o nounset echo FILE=${1-data1} cat > $FILE <<EOF first line second line a phone number is 555-1234 next line last line EOF echo " Data file $FILE:" cat $FILE echo echo " Results, nawk on Solaris:" myvariable="phone" nawk -v "v=$myvariable" ' BEGIN { print "v = " v } $0 ~ v { print ; outside = NR + 1; next } NR == outside { print ; exit } ' $FILE echo echo " sed on Solaris:" sed -n "/$myvariable/{p n p } " $FILE exit 0
See man pages, etc. ... cheers, drlCode:$ ./s1 (Versions displayed with local utility "version") SunOS 5.10 GNU bash 3.00.16 sed - ( /usr/xpg4/bin/sed Aug 9 2005 ) nawk - ( /usr/bin/nawk Jan 8 2007 ) Data file data1: first line second line a phone number is 555-1234 next line last line Results, nawk on Solaris: v = phone a phone number is 555-1234 next line sed on Solaris: a phone number is 555-1234 next line
( Edits 1,2: corrected sequence on sed Solaris )Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote