Unix Awk Command - unix

Awk, I am new this this command, I know it can list out the text file with condition, but i have no idea how to list them when there is a "," in between the text, how do you count the "," in as $1.
but if its email, email won't show for some reason, I am thinking maybe I should include the "," ?, i am not sure how to solve the problem, and don't know what the problem is.
for example i want to show customerid and customersname, i will use:
awk'{print $1,$2}'
Customerid, customersname, email
12312322, MIKE, example#gmail.com
51231221, CALVIN, example2#gmail.com
91234232, LISA, example3#gmail.com
12359432, DICK, example4#gmail.com
94123432, ORAN, example5#gmail.com
63242333, KEVIN, example6#gmail.com

You want to use the comma as separator? Use -F like that:
awk -F, '{print $1,$2}'
If you want comma and spaces as separator you can use a regex:
awk -F',[[:space:]]*' '{print $1,$2}'

I'm not sure whether I got your question properly. You can specifiy the input field separator using the -F command line option:
awk -F, '{print $1, $2}' your.csv
Output:
Customerid customersname
12312322 MIKE
51231221 CALVIN
91234232 LISA
12359432 DICK
94123432 ORAN
63242333 KEVIN

simply using FS:
awk 'BEGIN { FS="," } {print $1,$2}'
from man awk:
7. Builtin-variables
The following variables are built-in and initialized before program execution.
...
FS splits records into fields as a regular expression.
...

Here is the code needed
awk -F "," '{print $1,$2}' input.txt
Output:
Customerid, customersname
12312322, MIKE
51231221, CALVIN
91234232, LISA
12359432, DICK
94123432, ORAN
63242333, KEVIN
Explanation:
-F = Field separator
"," = using comma because columns are separated by ,
'{print $1,$2}' = display first and second column
input.txt = the file you want to pass
Hope its help.

Related

Unix To display student records

Contents of sample input file(input.txt) - starting from following line,
Name|Class|School Name
Deepu|First|Meridian
Neethu|Second|Meridian
Sethu|First|DAV
Theekshana|Second|DAV
Teju|First|Sangamithra
I need to output the details of the student with the school name Sangamithra
in the below format. I am new to unix. So I need help.
Desired output:
Sangamithra|First|Teju
I think you are looking something like this one.
awk -F\| '{print $3"|"$2"|"$1}' filename
School Name|Class|Name
Meridian|First|Deepu
Meridian|Second|Neethu
DAV|First|Sethu
DAV|Second|Theekshana
Sangamithra|First|Teju
If you're just interested in the output, this can be achieved using grep:
grep "Sangamithra" input.txt
If you want the name to be first, you might need awk (tested):
grep "Sangamithra" input.txt | awk -F "|" '{print $3"|"$1"|"$2}'

awk write in file with column separator

I am reading a file and writing first 2 columns into an output file.
I want write with "," as a column separator
I tried with
awk -F"," -OFS"|" '{print $1 , $2}' filename
The output file doesn't have | separator
Thanks
Pratik
Yes it will not print since you didn't write it properly. Following are the 2 ways to mention OFS in any awk program.
1st way: By using -v OFS="|" mention it as a variable.
awk -F"," -v OFS="|" '{print $1,$2}' filename
2nd way: Use BEGIN section of awk for mentioning it(which is recommended too).
awk 'BEGIN{FS=",";OFS="|"}{print $1,$2}' filename
3rd way: As per ghoti's comment adding 1 more way of assigning value for OFS here. We could assign it before mentioning Input_file names too by doing this we could set different OFS values for different Input_file(s)(since awk could read multiple Input_files so it can help in those kind of situations). Eg-->
awk '{print $1,$2}' FS="," OFS="|" Input_file1 FS=":" OFS=";" Input_file2
In above command for Input_file1 FS is , and OFS is | and for Input_file2 FS is : and OFS is ;. Thanks to ghoti sir for mentioning this in comments :)

delete first and last hyphen character from each column

I am trying to remove the first and last characters from two separate columns prior to them being saved to a file. The characters I need to remove are the hyphens. Due to hyphens in the results, I am unable to just remove all of them. Is there a more effective way to use awk for this?
my current thoughts are something similar to this command.
cat file.txt | awk -F '|' '{print $2, $4}' | sed 's/.//;s/.$//' > newfile.txt
file example
1-|-40939-23-|-column-3-|-column-4-|
2-|-9832651-23-|-column-3-|-column-4-|
current output
40939-23- -column-4
9832651-23- -column-4
desired output
40939-23 column-4
9832651-23 column-4
$ awk -F'-[|](-|$)' '{print $2, $4}' file
40939-23 column-4
9832651-23 column-4
Could you please try following and let me know if this helps.
awk -F"|" '{gsub(/^-|-$/,"",$2);gsub(/^-|-$/,"",$(NF-1));print $2,$(NF-1)}' Input_file
Solution 2nd: Using field numbers considering that your Input_file will be always same.
awk 'BEGIN{FS="[-|]";OFS="-"}{print $4 OFS $5 " " $12 OFS $13}' Input_file

Combining two awk commands in single command

I want to combine these two command and want to invoke single command
In first command i am storing 4th column of x.csv(Separator ,) file in z.csv file.
awk -F, '{print $4}' x.CSV > z.csv
In second command, i want to find out unique first-column value of z.csv(Separator-space) file.
awk -F\ '{print $1}' z.csv|sort|uniq
I want to combine these two command in single command,How can i do that?
Pipe the output of the first awk to the second awk:
awk -F, '{print $4}' x.CSV | awk -F\ '{print $1}' |sort|uniq
or, as Avinash Raj suggested,
awk -F, '{print $4}' x.CSV | awk -F\ '{print $1}' | sort -u
Assuming that the content of z.csv is actually wanted, rather than just an artefact of the way you're currently implementing your program, then you can use:
awk -F, '{ print $4 > "z.csv"
split($4, f, " ")
f4[f[1]] = 1
}
END { for (i in f4) print i }' x.CSV
The split function breaks field 4 on spaces, and (associative) array f4 records the key value. The loop at the end prints out the distinct values, unsorted. If you need them sorted, you can either use GNU awk's built-in sort functions or (if you don't have an awk with built-in sort functions) write your own in awk, or pipe the output to sort.
With GNU awk, you can replace the END block with:
END { asorti(f4); for (i in f4) print f4[i] }
If you don't want the z.csv file, then (a) you could have used a pipe in the first place, and (b) you can simply remove the print $4 > "z.csv" line.
awk '{split($4,b," "); a[b[1]]=1} END { for( i in a) print i }' FS=, x.CSV
This does not sort the data, but it's not clear if you actually want it sorted or merely needed that to get unique entries. If you do want it sorted, pipe it to sort.

Forcing the order of output fields from cut command

I want to do something like this:
cat abcd.txt | cut -f 2,1
and I want the order to be 2 and then 1 in the output. On the machine I am testing (FreeBSD 6), this is not happening (its printing in 1,2 order). Can you tell me how to do this?
I know I can always write a shell script to do this reversing, but I am looking for something using the 'cut' command options.
I think I am using version 5.2.1 of coreutils containing cut.
This can't be done using cut. According to the man page:
Selected input is written in the same order that it is read, and is
written exactly once.
Patching cut has been proposed many times, but even complete patches have been rejected.
Instead, you can do it using awk, like this:
awk '{print($2,"\t",$1)}' abcd.txt
Replace the \t with whatever you're using as field separator.
Lars' answer was great but I found an even better one. The issue with his is it matches \t\t as no columns. To fix this use the following:
awk -v OFS=" " -F"\t" '{print $2, $1}' abcd.txt
Where:
-F"\t" is what to cut on exactly (tabs).
-v OFS=" " is what to seperate with (two spaces)
Example:
echo 'A\tB\t\tD' | awk -v OFS=" " -F"\t" '{print $2, $4, $1, $3}'
This outputs:
B D A

Resources