|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
help needed with grep
Hi,
My file contains the following info: 0x00201800 0x172165 0x002bc3 32 0 0 0 0x00211800 0x172179 0x002bc3 32 0 0 0 0x00301800 0x972165 0x0025c3 48 0 0 0 0x00311800 0x972179 0x0025c3 48 0 0 0 0x00381800 0xd72165 0x0077c2 56 0 1 0 0x00391800 0xd72179 0x0077c2 56 0 1 0 When i do: grep -w 32 <filename> | awk '{print $2}' I get 2 values: 0x172165 0x172179 1. How can I assign these 2 values to 2 different variables? 2. Else can I grep one line at a time? Thanks, Yogesh. |
|
#2
|
|||
|
|||
|
I don't know what you mean by assigning them to two different variables or processing grep one line at a time...
If you want to process your result set you could do a for loop: Code:
for X in `grep -w 32 textfile |awk '{print $2}'`
do
echo Processing value $X
done
Processing value 0x172165
Processing value 0x172179
You could process the text using awk: Code:
awk '{
if($4==32)
{print "Processing line "$0}
else
{print "Ignoring line "$0}
}' textfile
Processing line 0x00201800 0x172165 0x002bc3 32 0 0 0
Processing line 0x00211800 0x172179 0x002bc3 32 0 0 0
Ignoring line 0x00301800 0x972165 0x0025c3 48 0 0 0
Ignoring line 0x00311800 0x972179 0x0025c3 48 0 0 0
Ignoring line 0x00381800 0xd72165 0x0077c2 56 0 1 0
Ignoring line 0x00391800 0xd72179 0x0077c2 56 0 1 0
...and you can parse the file into a for loop: Code:
cat textfile |while read VAR1 VAR2 VAR3 VAR4 VAR5 VAR6 VAR7
do
if [[ $VAR4 -eq 32 ]]; then
echo Processing line $VAR1 $VAR2 $VAR3 $VAR4 $VAR5 $VAR6 $VAR7
else
echo Ignoring line $VAR1 $VAR2 $VAR3 $VAR4 $VAR5 $VAR6 $VAR7
fi
done
Processing line 0x00201800 0x172165 0x002bc3 32 0 0 0
Processing line 0x00211800 0x172179 0x002bc3 32 0 0 0
Ignoring line 0x00301800 0x972165 0x0025c3 48 0 0 0
Ignoring line 0x00311800 0x972179 0x0025c3 48 0 0 0
Ignoring line 0x00381800 0xd72165 0x0077c2 56 0 1 0
Ignoring line 0x00391800 0xd72179 0x0077c2 56 0 1 0
|
|
#3
|
|||
|
|||
|
i know: unix piping is magnetic += fantastic
andy's first is fast: awk has less to do, the job is made by grep, i prefer the second, even if less performant it's more intelligent, the third one is for greens and {dos,win}-experts. Q: in this case are piping needed ? A: NO assumed your line: >>> 0x00201800 0x172165 0x002bc3 32 0 0 0 <<< sed -n 's/[ST]*\(.*\)[ST]*[ST]\(.*\).*[ST]*[ST]32[ST]*[ST].*/\2/p' filename >output substitute ST by a SPACE followed by a TAB character should do the job. (untested, at the moment my *nix is down) |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > help needed with grep |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|