First, you are telling awk that your field separator is a comma, but some commas in your input file are not field separators.
Second, you are telling awk to look for lines where the string string 1 is the 5th field in the line; but that string is never between commas in your input (even if we only counted the commas that are meant to be field separators. Each of your intended fields contains double quotes and the strings "string 1" and string 1 are not the same. To look for a string containing quotes, you have to put escaped quotes in your match string. For example:
The output you got from the command:
may have seemed strange to you, but it is exactly what I would have expected. Note the difference between $5="string 1" and $5=="string 1". With a single equal sign, you set the 5th field to string 1 rather than testing if the 5th field was string 1.
Try the following:
Using double quote as the field separator, $1 and $NF will be empty strings, other odd fields will be commas, and the even fields will be the data between pairs of double quotes. So $2 will be the data between the 1st pair of double quotes, $4 will be the data between the second pair of double quotes, ..., $10 will be the data between the 5th pair of double quotes, ...
This User Gave Thanks to Don Cragun For This Post:
Hi all,
I am new to shell script.I need your help to write a shell script.
I need to write a shell script to extract data from a .csv file where columns are ',' separated.
The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.... (3 Replies)
input.csv:
Field1,Field2,Field3,Field4,Field4
abc ,123 ,xyz ,000 ,pqr
mno ,123 ,dfr ,111 ,bbb
output:
Field2,Field4
123 ,000
123 ,111
how to fetch the values of Field4 where Field2='123'
I don't want to fetch the values based on column position. Instead want to... (10 Replies)
cat sample.csv
ID,Name,no
1,AAA,1
2,BBB,1
3,AAA,1
4,BBB,1
cut -d',' -f2 sample.csv | sort | uniq
this gives only the 2nd column values
Name
AAA
BBB
How to I get all the columns of CSV along with this? (1 Reply)
Hi All,
I am trying to select the rows in a fixed width file based on values in the columns.
I want to select only the rows if column position 3-4 has the value AB
I am using cut command to get the column values. Is it possible to check if cut -c3-4 = AB is true then select only that... (2 Replies)
Hello,
I have a script that is generating a tab delimited output file.
num Name PCA_A1 PCA_A2 PCA_A3
0 compound_00 -3.5054 -1.1207 -2.4372
1 compound_01 -2.2641 0.4287 -1.6120
3 compound_03 -1.3053 1.8495 ... (3 Replies)
Hello
I have a tab text file with many columns and have to filter rows ONLY if column 22 has the value of '0', '1', '2' or '3' (out of 0-5).
If Column 22 has value '0','1', '2' or '3' (highlighted below), then remove anything less than 10 and greater 100 (based on column 5) AND remove anything... (1 Reply)
Hello everyone,
I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this:
20170628-23:25:01,1,0,0,1,1,1,1,55,55,1
20170628-23:30:01,1,0,0,1,1,1,1,56,56,1
20170628-23:35:00,1,0,0,1,1,2,1,57,57,2
20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
I have csv file with 30, 40 columns
Pasting just three column for problem description
I want to filter record if column 1 matches CN or DN then,
check for values in column 2 if column contain 1235, 1235 then in column 3 values must be sequence of 2345, 2345
and if column 2 contains 6789, 6789... (5 Replies)
Discussion started by: as7951
5 Replies
LEARN ABOUT FREEBSD
join
JOIN(1) BSD General Commands Manual JOIN(1)NAME
join -- relational database operator
SYNOPSIS
join [-a file_number | -v file_number] [-e string] [-o list] [-t char] [-1 field] [-2 field] file1 file2
DESCRIPTION
The join utility performs an ``equality join'' on the specified files and writes the result to the standard output. The ``join field'' is
the field in each file by which the files are compared. The first field in each line is used by default. There is one line in the output
for each pair of lines in file1 and file2 which have identical join fields. Each output line consists of the join field, the remaining
fields from file1 and then the remaining fields from file2.
The default field separators are tab and space characters. In this case, multiple tabs and spaces count as a single field separator, and
leading tabs and spaces are ignored. The default output field separator is a single space character.
Many of the options use file and field numbers. Both file numbers and field numbers are 1 based, i.e., the first file on the command line is
file number 1 and the first field is field number 1. The following options are available:
-a file_number
In addition to the default output, produce a line for each unpairable line in file file_number.
-e string
Replace empty output fields with string.
-o list
The -o option specifies the fields that will be output from each file for each line with matching join fields. Each element of list
has either the form file_number.field, where file_number is a file number and field is a field number, or the form '0' (zero), repre-
senting the join field. The elements of list must be either comma (',') or whitespace separated. (The latter requires quoting to
protect it from the shell, or, a simpler approach is to use multiple -o options.)
-t char
Use character char as a field delimiter for both input and output. Every occurrence of char in a line is significant.
-v file_number
Do not display the default output, but display a line for each unpairable line in file file_number. The options -v 1 and -v 2 may be
specified at the same time.
-1 field
Join on the field'th field of file1.
-2 field
Join on the field'th field of file2.
When the default field delimiter characters are used, the files to be joined should be ordered in the collating sequence of sort(1), using
the -b option, on the fields on which they are to be joined, otherwise join may not report all field matches. When the field delimiter char-
acters are specified by the -t option, the collating sequence should be the same as sort(1) without the -b option.
If one of the arguments file1 or file2 is '-', the standard input is used.
EXIT STATUS
The join utility exits 0 on success, and >0 if an error occurs.
COMPATIBILITY
For compatibility with historic versions of join, the following options are available:
-a In addition to the default output, produce a line for each unpairable line in both file1 and file2.
-j1 field
Join on the field'th field of file1.
-j2 field
Join on the field'th field of file2.
-j field
Join on the field'th field of both file1 and file2.
-o list ...
Historical implementations of join permitted multiple arguments to the -o option. These arguments were of the form
file_number.field_number as described for the current -o option. This has obvious difficulties in the presence of files named 1.2.
These options are available only so historic shell scripts do not require modification and should not be used.
SEE ALSO awk(1), comm(1), paste(1), sort(1), uniq(1)STANDARDS
The join command conforms to IEEE Std 1003.1-2001 (``POSIX.1'').
BSD July 5, 2004 BSD