LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 08-23-2010, 02:30 AM   #1
jestinjoy
Member
 
Registered: May 2004
Location: India
Distribution: Ubuntu 9.04, Debian Lenny
Posts: 121

Rep: Reputation: 22
awk help


Hi ,
I am beginner with awk. I have a file like this.

Code:
	.long sys_restart_syscall	/* 0 - old "setup()" system call, used for restarting */
	.long sys_exit
	.long sys_fork
	.long sys_read
	.long sys_write
	.long sys_open		/* 5 */
	.long sys_close
	.long sys_waitpid
	.long sys_creat
	.long sys_link
	.long sys_unlink	/* 10 */
I would like to extract only restart_syscall,exit,fork,read,... from the file( these are system calls and i gave the system call table as input). For this i tried to replace first _ with a space and tried printing $3 but what i got as ouput is given below. I only want the output of the { print $3} to be written to the new file. Please help

Code:
	.long sys restart_syscall	/* 0 - old "setup()" system call, used for restarting */

	.long sys exit
exit
	.long sys fork
fork
	.long sys read
read
	.long sys write
write
	.long sys open		/* 5 */
open
	.long sys close
close
	.long sys waitpid
waitpid
	.long sys creat
creat
	.long sys link
link
	.long sys unlink	/* 10 */
unlink

My code is as follows
Code:
#!/usr/bin/awk -f
BEGIN {FS = "_"}
	sub(/_/," ")
	{FS = " "}
	{print $3}
END {}
 
Old 08-23-2010, 03:17 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Well if I understand correctly, you wish to change the following:
Quote:
.long sys_close
into:
Quote:
close
Correct??

If so then you have 2 options, awk:
Code:
awk 'sub(/[^_]+_/,"")' file
or sed:
Code:
sed -r 's/[^_]+_//' file
Nice part about using sed is once you have what you like you can throw a '-i' at the start and the change will be made to the file.
Awk obviously you have to redirect and then perhaps rename.

Remembering also that both of the above will work on entire file, so you may need to edit it to only start and finish where you like
 
Old 08-23-2010, 05:31 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
Hi,

This worked for me.
Code:
awk '{print $2}' file| cut -c 5-
As grail suggested,

Code:
awk 'sub(/[^_]+_/,"")' file
sed -r 's/[^_]+_//' file
work fine. But does not take care of removing /* */ comments which are appended in few lines.

Last edited by vinaytp; 08-23-2010 at 05:35 AM.
 
Old 08-23-2010, 07:06 AM   #4
jestinjoy
Member
 
Registered: May 2004
Location: India
Distribution: Ubuntu 9.04, Debian Lenny
Posts: 121

Original Poster
Rep: Reputation: 22
re

@grail It worked fine.
@vinaytp as suggested your script removed comments too.

But I have another question. When I run my script(not command). It gives the old and new one in a single file. Can I do the same thing as u suggested using script. What I did is given below.
My code

Code:
#!/usr/bin/awk -f
BEGIN {}
	sub(/_/," ") 
	{print $3} 
END {}
Then I executed the following command
Code:
./test.awk table.txt > test.txt
What I got As output is
Code:
	.long sys restart_syscall	/* 0 - old "setup()" system call, used for restarting */
restart_syscall
	.long sys exit
exit
	.long sys fork
fork
	.long sys read
read
	.long sys write
write
	.long sys open		/* 5 */
open
	.long sys close
close
	.long sys waitpid
waitpid
	.long sys creat
creat
	.long sys link
link
	.long sys unlink	/* 10 */
I want the things as given by you only not the .long lines. What should I do for this.
 
Old 08-23-2010, 07:20 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
The default action for any true statement if no other action is provided is to print.
The BEGIN and END in your script are redundant and do nothing.
Code:
sub(/_/," ")
This line says to remove the first underscore found in each line. The return from sub is the number of changes made, ie 1 or zero. All lines that return 1
will be printed.
Code:
{print $3}
Then you go on to print the third field as well.

Hence the double up of data.
 
Old 08-23-2010, 07:28 AM   #6
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
You have modified the script given by grail which doesn's statisfy your requirement. You just have to use this way

Code:
#!/usr/bin/awk -f
sub(/[^_]+_/,"")
Now Try
Code:
./test.awk table.txt > test.txt
 
Old 08-23-2010, 10:01 AM   #7
jestinjoy
Member
 
Registered: May 2004
Location: India
Distribution: Ubuntu 9.04, Debian Lenny
Posts: 121

Original Poster
Rep: Reputation: 22
Wink

Quote:
Originally Posted by grail View Post
The default action for any true statement if no other action is provided is to print.
The BEGIN and END in your script are redundant and do nothing.
Code:
sub(/_/," ")
This line says to remove the first underscore found in each line. The return from sub is the number of changes made, ie 1 or zero. All lines that return 1
will be printed.
Code:
{print $3}
Then you go on to print the third field as well.

Hence the double up of data.
This solved my problem. I was unaware of the default printing thing.

@vinaytp Your code helped me to remove comment as well. Thanks for the help
 
  


Reply

Tags
awk, scripting


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
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 05:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:50 AM.

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