LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-26-2009, 02:54 AM   #1
ZAMO
Member
 
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.

Code:
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.

Code:
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 02:56 AM.
 
Old 02-26-2009, 04:13 AM   #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
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
Code:
$ 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:
Code:
$ 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.
 
  


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 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 08:48 PM
Is it possible to manipulate strings INSIDE awk scripts? Jude Terror Linux - Newbie 2 12-09-2008 04:42 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
RE in commands like match() inside awk. stalin.varanasi Linux - Newbie 2 12-12-2007 11:31 PM
awk inside a makefile linux.fob Programming 2 10-12-2005 04:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:04 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