Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 {}
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
@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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.