Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 02-26-2009, 03:54 AM   #1
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
Sed inside awk

Hi all,

Is it possible to insert a sed inside awk?? Am trying to output the awk contents to 2 different files.

cat myfile|awk -F "*" '{print $1F >> "tfile1" ; print $NF >> "lastfile"} '
In this i need to do a sed in my first print pattern of awk . So am trying with the following and failing. Is there a way to do it.

cat myfile|awk -F "*" '{print $1F |sed 's/#//g' >> "tfile1" ; print $NF >> "lastfile"} '
I want to remove the # in file in my first awk. Is there a way to use sed inside awk or am I trying with something wrong.

Last edited by ZAMO; 02-26-2009 at 03:56 AM.
Old 02-26-2009, 05:13 AM   #2
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
Indeed you can use external commands within an awk program and share their output, but you don't really need them to achieve your task. In awk there are some useful functions to manipulate string. For example the sub and the gsub functions, act the same as sed. They alter the content of the fields, so that when you print them out, you will see the new value of the string.

Suppose you have the file
$ cat myfile
#this is a commented line
this is a line of code
#another commented line
You can use the gsub function in the following way:
$ awk '{gsub(/#/,"",$1); print}' myfile
this is a commented line
this is a line of code
another commented line
the value of the first field has been altered substituting the part of the string matching the regexp /#/, with the replacement string "". The subsequent print command, prints out the line with the altered field. Take a look at the string manipulation functions on the AWK Programming Guide for more details. Hope this helps.


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use grep inside awk? Helene Programming 10 09-29-2015 09:48 PM
Is it possible to manipulate strings INSIDE awk scripts? Jude Terror Linux - Newbie 2 12-09-2008 05:42 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
RE in commands like match() inside awk. stalin.varanasi Linux - Newbie 2 12-13-2007 12:31 AM
awk inside a makefile linux.fob Programming 2 10-12-2005 05:57 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:55 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration