The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to append spaces(say 10 spaces) at the end of each line based on the length of th prathima UNIX for Dummies Questions & Answers 17 01-28-2009 04:10 PM
Print to ps2pdf print queue Sean_69 SUN Solaris 2 10-22-2007 11:00 AM
Print to a ps2pdf print queue. Sean_69 UNIX for Dummies Questions & Answers 1 10-22-2007 10:58 AM
Print Problem in UNIX. Need to know the option to specify the print paper size ukarthik HP-UX 1 06-07-2007 09:35 AM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 02:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-16-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
Arrow How to print arguments along with spaces using awk

Hi All,

file_1.txt contains

aaa bbbb hhhh
vvvvv mmmmm iiiii

What i want is to search for the first coloumn of each line using awk.i.e as below:

awk '/aaa/ {printf(<>)}' file_1.txt

The print part (<>) should contain all the values(or coloumns ) in the particular line(here it is: bbbb hhhh) including the spaces between each arguments.Given that the "aaa" should not be printed.

In short my output after running the above awk should be
bbbb hhhh

If it is the "vvvv" am searching for then the result should be
mmmmm iiiii

Thanks in advance
JS
  #2 (permalink)  
Old 01-16-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
Code:
awk '/aaa/ {print $2, $3}' file_1.txt
  #3 (permalink)  
Old 01-16-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
This command prints only one single space between each arguments.Not all the spaces!!!
  #4 (permalink)  
Old 01-16-2008
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
jisha you need to be more polite , Yogesh was trying to help u.

First , your regex in wrong it'll macths all lines that contains the "aaa" not only first column,...
anyway many ways to do , one could be:
Code:
awk '/^aaa/{sub(/^aaa */,"",$0);print}' file
  #5 (permalink)  
Old 01-17-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
Dear God .. I never meant anything unpolite ..I just said how it worked out .. And i was in a very tight situation yesterday that i missed to put the thanks .. Sorry for that. .
  #6 (permalink)  
Old 01-17-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
Quote:
Originally Posted by Klashxx View Post
jisha you need to be more polite , Yogesh was trying to help u.

First , your regex in wrong it'll macths all lines that contains the "aaa" not only first column,...
anyway many ways to do , one could be:
Code:
awk '/^aaa/{sub(/^aaa */,"",$0);print}' file
Thank you Klashxx

but code doesnot print the spaces in the begining .. Also when i ran this in command promt other than the starting spaces everything else is working .. But am supposed to run this in a script where the pattern is received from a function call .. When i executed the script, i got error saying unamatched ..

The fields in the file are unique ..Nothing is repeated ..Also the pattern for matching is always at teh first of coloumn in my file ..

Once again thanks for the effort ...
JS
  #7 (permalink)  
Old 01-16-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Code:
awk 'sub(/^aaa/,"")' filename
I'm not sure if you want the space after the pattern (if any),
in that case:

Code:
awk 'sub(/^aaa */,"")' filename

Last edited by radoulov; 01-16-2008 at 09:37 AM.. Reason: Use nawk or /usr/xpg4/bin/awk on Solaris.
Closed Thread

Bookmarks

Tags
awk, regex

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0