awk basic issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk basic issue
# 1  
Old 11-30-2008
awk basic issue

Hi all,


I have an awk basic question.


file.text


Our Location:
Our home has light yellow siding,
and is a duplex on Main Street,
across from the High School,
and across the lane from the Health Center


If I run:


cat file.txt | awk '{print $2}' | grep „Location“


then the script is working fine.


But if the first line on the file.text is changed to „My and your Location“ then the script does't work. Is there any posibility to give the awk a regular expression, for expample: cat file1 | awk '{print $Location}' ?
Is there any posibility that I don't have to defin the exact column number?
# 2  
Old 11-30-2008
cat and grep is redundant if you use awk or sed, this should print the lines that match the pattern:

Code:
awk '/Location/' file.txt

With sed:

Code:
sed -n  '/Location/p' file.txt

# 3  
Old 11-30-2008
Quote:
Originally Posted by research3
...But if the first line on the file.text is changed to „My and your Location“ then the script does't work. Is there any posibility to give the awk a regular expression....
Is there any possibility that I don't have to defin the exact column number?
grep was made for that
Code:
grep Location file

# 4  
Old 11-30-2008
Quote:
Originally Posted by danmero
grep was made for that
Code:
grep Location file

Sure! I have any trouble with profession blindness.Smilie
# 5  
Old 11-30-2008
yes, but the standard output is a line and not a single word.
# 6  
Old 11-30-2008
maybe I ask the question wrong,
I mean how can I grep one value (egular expression exp. Loca* ) in an line if the position (first or second column) is not defined?
And the result is only the word.

With this process I will test if the value is available "match" or not?
But maybe is sed the better tool for this.
# 7  
Old 12-01-2008
Quote:
Originally Posted by research3
maybe I ask the question wrong,
Yes, you do Smilie
Quote:
Originally Posted by research3
I mean how can I grep one value (egular expression exp. Loca* ) in an line if the position (first or second column) is not defined?
Now you want first column or second column or not defined? Smilie

Quote:
Originally Posted by research3
And the result is only the word.
Code:
awk '/Location/{print "BINGO"}' file
or ..
sed -n '/Location/s/.*/BINGO/p' file
or..
grep -q Location file  && echo BINGO


Last edited by danmero; 12-01-2008 at 01:17 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confusing of some basic awk

1. increase file space first, double space a file: awk '1;{print ""}' I probably can understand it:print a blank line every time.But when I read triple space a file I am confused: awk '1;{print "\n"}' doesn't it meaning print a blank line every time too? 2. number each line of file, but... (6 Replies)
Discussion started by: hhdzhu
6 Replies

2. UNIX for Dummies Questions & Answers

Basic arithmetic operation with awk?

input: Name|Operation rec_10|1+2+2- Output: rec_10|1 Basically I am trying to calculate the result of "the path" in $3 where the operators follow the number and not preceding them like we normally do: rec_10: +1+2-2=1 But I realise (I am sure there is a good reason for that) that awk... (7 Replies)
Discussion started by: beca123456
7 Replies

3. UNIX for Dummies Questions & Answers

Basic awk...newbie quetion

Hi, I was trying to change the value of the 4th column (put '1' in the 4th column of each row). My awk command is: awk -F, '{$3=1;}1' OFS= input.txt > ./test_out.txt My input file is: a 1 2 31 b 4 5 61 c 7 8 91 My output file (test_out.txt)is: a 1 2 31 b 4 5 61 c 7 8 91 What... (4 Replies)
Discussion started by: pc2001
4 Replies

4. UNIX for Dummies Questions & Answers

Basic awk help

Im sure this is an easy question, but Ive tried and tried to get this to print all on one line and cant figure out why its not, so maybe someone can help awk '/AP/{sub(/:80/, "", $4);printf $4"\t"} /User-Agent/{sub(/^*:/,"");print};sub(/\.80/,"", $4);/Host/{sub(/^*:/,""); print}' What this... (10 Replies)
Discussion started by: sectech
10 Replies

5. Shell Programming and Scripting

Issue with basic Awk script

Here's a basic awk program I am trying to run. It shows no error but shows no result either too. If someone can look up and tell me what's wrong I will be obliged. Thanks. :) Code Snippet. #!/bin/bash awk '{ for (i = 1 ; i <= 3 ; i++) for ( j = 1 ; j <= 3 ; j++ ) { ... (2 Replies)
Discussion started by: mr.amitkc
2 Replies

6. Shell Programming and Scripting

basic awk questions

I find an script with awk sitting around. I went through some online manuals, but I can't figure out exactly how it works. I can't post the whole program. Not allowed. This is the line that is confusing me. I get when else is in the script grep -v "^REM " $1| grep -v "JUNK;" | awk -F" "... (2 Replies)
Discussion started by: guessingo
2 Replies

7. Shell Programming and Scripting

cron job issue..i hav read the basic threads already...

hi friends well m facing a different sort of issue in my cron. i hav set job like this 30 09 * * 1 /bin/backup14M 01 14 * * 1 /bin/backup14N 20 18 * * 1 /bin/backup14E that is for every Monday at three different times. but, first job executes well, later ones do not. I checked my... (13 Replies)
Discussion started by: oracle.test2
13 Replies

8. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies
Login or Register to Ask a Question