LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2008, 04:50 PM   #1
matt007
LQ Newbie
 
Registered: Jun 2008
Posts: 14

Rep: Reputation: 0
using awk substring function on a file in a bash script


My script is:

#!/bin/bash

awk 'BEGIN { substr($0,1,2) == "HI"}' filename >> output_file

I think $0 represents each line in "filename" and i want to match 2 charcter begining position 1.

I expect it to return all matching records into output_file...but it returns me a 0 for each line in "filename".

This works perfectly from command line....

where am i wrong....
 
Old 06-17-2008, 05:01 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by matt007 View Post
I expect it to return all matching records into output_file...but it returns me a 0 for each line in "filename".
Do you want to print all lines matching the condition? You have to simply use an expression, not an action. That is, don't put the brackets (nor the BEGIN section) and for each line matching the condition, the default action (print $0) will be executed.
Code:
awk 'substr($0,1,2) == "HI"' filename >> output_file
The same can be accomplished using grep
Code:
grep ^HI filename >> output_file

Last edited by colucix; 06-17-2008 at 05:09 PM. Reason: proposed alternative
 
Old 06-17-2008, 08:46 PM   #3
matt007
LQ Newbie
 
Registered: Jun 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Can i search for multiple keywords in --

1. ) egrep '^HI|^AZ' filename >> output_file:
Is the above a valid command to return all lines in the file that begin with an "HI" or "AZ".


2. ) In AWK :: awk 'substr($0,1,2) == "HI"' filename >> output_file
Can i look for more than 1 keyword in the same command i.e in just one line... (say AZ in addition to HI)


3. ) Can i inverse the AWK...(like we run grep and egrep with the -v switch)
say 2.) above returns all lines starting with "HI"....
How do i get all lines NOT starting with (HI and AZ)
 
Old 06-17-2008, 09:17 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you want to use awk, there's no need to use egrep
Code:
awk '/^HI|AZ/' file
inverse
Code:
awk '!/^HI|AI/' file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 02:16 PM
Problem with AWK in a BASH script geek_man Programming 4 01-20-2008 08:16 PM
Putting awk in bash script the_imax Programming 9 05-12-2007 04:50 PM
can awk see bash script arguments ? sharapchi Programming 7 12-14-2006 09:03 PM
Bash script question (grep and awk) hamish Linux - Software 6 04-06-2005 04:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration