LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 03: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, 04:01 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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 04:09 PM. Reason: proposed alternative
 
Old 06-17-2008, 07: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, 08: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



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 01:16 PM
Problem with AWK in a BASH script geek_man Programming 4 01-20-2008 07:16 PM
Putting awk in bash script the_imax Programming 9 05-12-2007 03:50 PM
can awk see bash script arguments ? sharapchi Programming 7 12-14-2006 08:03 PM
Bash script question (grep and awk) hamish Linux - Software 6 04-06-2005 03:14 PM

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

All times are GMT -5. The time now is 09:24 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
Open Source Consulting | Domain Registration