awk delete line if number of rows = some number [duplicate] - shell

This question already has answers here:
Save modifications in place with awk
(7 answers)
Closed 7 years ago.
This is my awk code:
awk -F"," 'NF!= 8' myfile.csv
How can I delete only lines that have 8 fields.

Here you go (this prints lines with 8 fields as originally asked)
awk -F, 'NF==8' myfile.csv
The question changed, you want to remove the lines with 8 fields. One way to do this
awk -F, 'NF!=8' myfile.csv > temp && mv temp mvfile.csv
NB. updated as per comments

Related

Getting unique values from column in a csv file [duplicate]

This question already has answers here:
awk to remove duplicate rows totally based on a particular column value
(6 answers)
Closed 4 years ago.
I have the following input:
no,zadrar,MENTOR,rossana#xt.com,AGRATE
no,mittalsu,MENTOR,rossana#xt.com,GREATER NOIDA
no,abousamr,CADENCE,selim#xt.com,CROLLES
no,lokinsks,MENTOR,sergey#xt.com,CROLLES
no,billys,MENTOR,billy#xt.com,CROLLES
no,basiles1,CADENCE,stephane#xt.com,CASTELLETTO
no,cesaris1,CADENCE,stephane#xt.com,CROLLES
I want to get only the lines where column 4 is unique:
no,abousamr,CADENCE,selim#xt.com,CROLLES
no,lokinsks,MENTOR,sergey#xt.com,CROLLES
no,billys,MENTOR,billy#xt.com,CROLLES
I tried with:
awk -F"," '{print $4}' $vendor.csv | sort | uniq -u
But I get:
selim#xt.com
sergey#xt.com
billy#xt.com
You can use simply the options provided by the sort command:
sort -u -t, -k4,4 file.csv
As you can see in the man page, option -u stands for "unique", -t for the field delimiter, and -k allows you to select the location (key).
Could you please try following(reading Input_file 2 times).
awk -F',' 'FNR==NR{a[$4]++;next} a[$4]==1' Input_file Input_file

How to iterate through line and check needed part? [duplicate]

This question already has an answer here:
How can I retrieve an entry from /etc/passwd for a given username?
(1 answer)
Closed 5 years ago.
I have this line
Username:x:120:101:somethingsomething
and I need to get the '101' part after the third ':', how can I do that?
do I use grep or sed?
cut -d':' -f4 /etc/passwd
awk, only with string:
mstr="Username:x:120:101:somethingsomething"; awk -F: '{print $4}' <<< "$mstr"

Unique entry set in the first column of all csv files under directory [duplicate]

This question already has answers here:
Is there a way to 'uniq' by column?
(8 answers)
Closed 7 years ago.
I have a list of comma separated files under the directory. There are no headers, and unfortunately they are not even the same length for each row.
I want to find the unique entry in the first column across all files.
What's the quickest way of doing it in shell programming?
awk -F "," '{print $1}' *.txt | uniq
seems to only get uniq entries of each files. I want all files.
Shortest is still using awk (this will print the row)
awk -F, '!a[$1]++' *.txt
to get just the first field
awk -F, '!a[$1]++ {print $1}' *.txt

Extract Information From File Name in Bash [duplicate]

This question already has answers here:
How to split a string into an array in Bash?
(24 answers)
Closed 7 years ago.
Suppose I have a file with a name ABC_DE_FGHI_10_JK_LMN.csv. I want to extract the ID from the file-name i.e. 10 with the help of ID position and file-name separator. I have following two inputs
File-name_ID_Position=4; [since 10 is at fourth position in file-name]
File-name_Delimiter="_";
Here ID can be numeric or alpha-numeric. So how extract the 10 from above file with the help of above two inputs. How to achieve this in bash?
Instead of writing a regex in bash, I would do it with awk:
echo 'ABC_DE_FGHI_10_JK_LMN.csv' | awk -F_ -v pos=4 '{print $pos}'
or if you want the dot to also be a delimiter (requires GNU awk):
echo 'ABC_DE_FGHI_10_JK_LMN.csv' | awk -F'[_.]' -v pos=4 '{print $pos}'

How to pass variable to awk [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using awk with variables
The following command is wrong, the point is I want to use $curLineNumber in awk, how can I do it? Any solution?
curLineNumber = 3
curTime=`ls -l | awk 'NR==$curLineNumber {print $NF}'`
Thanks
curTime=$(ls -l | awk -v line=$curLineNumber 'NR == line { print $NF }'
The -v option is used to specify variables initialized on the command line. I chose the name line for the awk variable.

Resources