Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-07-2010, 05:35 AM   #1
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Rep: Reputation: 17
awk problem

i did the following script

echo Every good boy is bad boy | awk '{first=substr($0,1,2)}{second=substr($0,8)}{printf("%s%s\n",first,second)}'
The result is Evood boy is bad boy

How to get the following result

Evood boy is baoy (substr the d,<space> and b)

when i try this

echo Every good boy is bad boy| awk '{first=substr($0,1,2)}{second=substr($0,8)}{third=substr($5,1,2)}{fourth=substr($6,2)}{printf("%s%s%s%s\n",first,second,third,fourth)}'
im getting this

Evood boy is bad boybaoy

Please advice


Old 01-07-2010, 07:43 AM   #2
LQ Newbie
Registered: May 2009
Distribution: Slackware
Posts: 17
Blog Entries: 1

Rep: Reputation: 9
This works, (if not then it's a cut and paste problem from my side).

echo Every good boy is bad boy | awk '{first=substr($0,1,2);second=substr($0,7,4);third=substr($0,11,10);fourth=substr($0,24,2);}END{printf("%s%s%s%s\n",first,second,third,fourth)}'
The first 'substr' function returns 2 chars from string, starting at position 0, just to be clear, 'substr' doesn't delete chars from the string. To get what you want you have to extract and concatenate the desired chars from the strings.


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
is it awk -v or sub the problem ? sandlinux Linux - Newbie 4 08-13-2009 07:09 PM
Help with AWK problem RyudoBlaze Programming 3 04-24-2009 08:24 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
A problem about awk jackk294 Programming 4 07-13-2006 09:56 PM
AWK problem embsupafly Programming 13 08-21-2005 08:43 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:01 AM.

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