In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use awk to print file names on Linux/Unix. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Q: I want to use awk to print file names on Linux / class Unix systems. How do I print a file name using awk's BEGIN special mode? Can I use gawk/awk to print the name of the current input file? (LCTT translation note: readers * can have some background knowledge of awk, otherwise there will be some confusion when reading this article)
The name of the current input file is stored in the FILENAME variable. You can use FILENAME to display or print the current input file name, and if no file is specified on the command line, the value of FILENAME is-(standard input) (LCTT translation: press enter multiple times to see the effect). However, FILENAME is not defined in BEGIN special mode unless set by getline.
Use awk to print file names
The usage syntax is as follows:
Awk'{print FILENAME} 'fileNameHere awk' {print FILENAME}'/ etc/hosts
Because awk reads the file line by line, you may see multiple file names, to avoid this, you can use the following command: (LCTT: FNR represents the current number of records, valid only in the file)
Awk 'FNR = = 1 {print FILENAME}' / etc/passwd awk 'FNR = = 1 {print FILENAME}' / etc/hosts
Use awk's BEGIN special rules to print file names
Use the following syntax: (LCTT translation note: ARGV [I] represents the I parameter entered)
Awk 'BEGIN {print ARGV [1]}' fileNameHere awk 'BEGIN {print ARGV [1]} {print "someting or do something on data"} END {}' fileNameHere awk 'BEGIN {print ARGV [1]}' / etc/hosts
Sample output:
/ etc/hosts
However, ARGV [1] does not work every time, for example:
Ls-l / etc/hosts | awk 'BEGIN {print ARGV [1]} {print}'
You need to modify it as follows (assuming ls-l produces only one line of output):
Ls-l / etc/hosts | awk'{print "File:" $9 ", Owner:" $3 ", Group:" $4}'
Sample output:
File: / etc/hosts, Owner:root, Group: root handles multiple file names specified by wildcards
Use the following example syntax:
Awk'{print FILENAME; nextfile}'* .c awk 'BEGIN {print "Starting..."} {print FILENAME; nextfile} END {print ".done"}' * .conf
Sample output:
Starting...blkid.confcryptconfig.confdhclient6.confdhclient.confdracut.confgai.confgnome_defaults.confhost.confidmapd.confidnalias.confidn.confinsserv.confiscsid.confkrb5.confld.so.conflogrotate.confmke2fs.confmtools.confnetscsid.confnfsmount.confnscd.confnsswitch.confopenct.confopensc.confrequest-key.confresolv.confrsyncd.confsensors3.confslp.confsmartd.confsysctl.confvconsole.confwarnquota.confwodim.confxattr.confxinetd.confyp.conf....DONE
Nextfile tells awk to stop processing the current input file. The next input record is read from the next input file. For more information, see the awk/gawk command man page:
Man awk man gawk, thank you for your reading! This is the end of the article on "how to use awk to print file names on Linux/Unix". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.