cut or awk command to print first field of first row
Specify NR
if you want to capture output from selected rows:
awk 'NR==1{print $1}' /etc/*release
An alternative (ugly) way of achieving the same would be:
awk '{print $1; exit}'
An efficient way of getting the first string from a specific line, say line 42, in the output would be:
awk 'NR==42{print $1; exit}'
Match a string from file and print only the first row that matching
Could you please try following.
awk '/c0p0d1/{print;exit}' Input_file
Explanation: I am searching string in each line and when a match is found I am printing the line and exiting ASAP since we need not to read file unnecessary. Exiting from program will make it faster too.
AWK Formatting Using First Row as a Header and Iterating by column
It is not clear where do instance
and type_instance=interrupt
come from in your final desired format. Otherwise awk code below should work.
Note: it doesn't strip %
from tag values and prints timestamp at end of line in seconds (append extra zeros if you want nanoseconds).
gawk -v HOSTNAME="$HOSTNAME" 'NR==1 {split($0,h,/[ \t\[\]]+/,s); for(i=0;i<length(h);i++){ h[i]=tolower(h[i]); };}; NR>1 { for(j=2;j<NF;j++) {k=2*j; printf("%s_value,host=%s,type=%s,type_instance=%s value=%s %s\n", h[k], HOSTNAME, h[k], h[k+1],$(j+1), mktime(substr($1,1,4)" "substr($1,5,2)" "substr($1,7,2)" "substr($2,1,2)" "substr($2,4,2)" "substr($2,7,2)));}}' mxmcaim01-20190228.tab
Display range of rows from first column with awk
Not fully clear though, in case you want to skip only first row then try following.
awk 'FNR>1' Input_file
OR to print 1st column use:
awk 'FNR>1{print $1}' Input_file
In case you do not know on which field Emp_No
will come and you want to look for its column number from 1st row AND DO NOT want to print the same column from rest of the row then try following.
awk '
BEGIN{
OFS="\t"
}
FNR==1{
for(i=1;i<=NF;i++){
if($i=="Emp_Name"){
val=i
next
}
}
}
{
for(i=1;i<=NF;i++){
if(i==val){
continue
}
else{
value=(value?value OFS:"")$i
}
}
print value
value=""
}
' Input_file
AWK - Match first column, print both lines if any values of the last field is greater than 1000
gawk '
{lines[$1] = lines[$1] $0 ORS; sum[$1] += $NF}
$NF > 1000 {p[$1] = 1}
END {
PROCINFO["sorted_in"] = "@val_num_desc"
for (key in sum)
if (p[key])
printf "%s", lines[key]
}
' file
Printing everything except the first field with awk
Assigning $1
works but it will leave a leading space: awk '{first = $1; $1 = ""; print $0, first; }'
You can also find the number of columns in NF
and use that in a loop.
AWK to print field $2 first, then field $1
A couple of general tips (besides the DOS line ending issue):
cat
is for concatenating files, it's not the only tool that can read files! If a command doesn't read files then use redirection like command < file
.
You can set the field separator with the -F
option so instead of:
cat foo | awk 'BEGIN{FS="|"} {print $2 " " $1}'
Try:
awk -F'|' '{print $2" "$1}' foo
This will output:
com.emailclient.account name1@gmail.com
com.socialsite.auth.accoun name2@msn.com
To get the desired output you could do a variety of things. I'd probably split()
the second field:
awk -F'|' '{split($2,a,".");print a[2]" "$1}' file
emailclient name1@gmail.com
socialsite name2@msn.com
Finally to get the first character converted to uppercase is a bit of a pain in awk
as you don't have a nice built in ucfirst()
function:
awk -F'|' '{split($2,a,".");print toupper(substr(a[2],1,1)) substr(a[2],2),$1}' file
Emailclient name1@gmail.com
Socialsite name2@msn.com
If you want something more concise (although you give up a sub-process) you could do:
awk -F'|' '{split($2,a,".");print a[2]" "$1}' file | sed 's/^./\U&/'
Emailclient name1@gmail.com
Socialsite name2@msn.com
Related Topics
How to Use Code Completion into Eclipse with Opencv
Rename Part of File Name Based on Exact Match in Contents of Another File
Execute Command Line and Return Command Output
Linux Task Schedule to Hour, Minute, Second
Cannot Use Wildcard in Kernel Module Makefile
Writing a Program for Hiding Processes from Ps Command Result
Docker Networking Namespace Not Visible in Ip Netns List
Can You Attach Amazon Ebs to Multiple Instances
How to Convert a PDF into Jpg with Command Line in Linux
How to Get the Nvidia Driver Version from the Command Line
Determine Target Isa Extensions of Binary File in Linux (Library or Executable)
Linux Custom Executable Globally Available
What Is the Use of Various Qt Platform Plugins
Rsync - Mkstemp Failed: Permission Denied (13)