How to extract the the data with delimiter [duplicate] - bash

This question already has answers here:
Extract specific columns from delimited file using Awk
(8 answers)
Closed 4 years ago.
I am parsing the csv file using while loop in shell script which has data in below format
ba04ba54,1234,MMS,"[""some"", ""somet2"", ""somet""]",21556,48834
code:
while IFS=, read -r id mvid conkey values cretime modified;
do
echo $id,$mvid,$conkey,$values,$cretime,$modified
but values is assigned with
"[""some""
instead of
"[""some"", ""somet2"", ""somet""]" how to achieve this using shell script

Could you please try following.
awk 'match($0,/\"\[\".*\]\"/){print substr($0,RSTART,RLENGTH)}' Input_file

Related

Cut columns two and three using bash [duplicate]

This question already has answers here:
How to extract one column of a csv file
(18 answers)
Closed 1 year ago.
I have a .csv file with three columns. I want to keep the first column only. I have been trying to work with a command similar to the one below.
cut -f 1,4 output.csv > output.txt
No matter what I do, my output remains the same- giving me all three columns. Can anyone give me some insight?
Thanks!
read file one line at a time, trim everything right of that first comma:
while read -r line; do echo ${line%%,*}; done < output.csv > output.txt

How can I remove the first word of each line of a program's output? [duplicate]

This question already has answers here:
How to delete the first column ( which is in fact row names) from a data file in linux?
(5 answers)
Unix , Delete first column using awk
(3 answers)
Closed 1 year ago.
This is output of speedtest-cli. I just need the specific values without the Download: and Upload: headers.
How can I do this?
Download: 8.34 Mbit/s
Upload: 0.69 Mbit/s
You can print only 2nd "word" using awk
awk '{print $2}'

How to extract string between IDs in shell [duplicate]

This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed 2 years ago.
I have a string that looks like this:
RG="#RG\tID:HS2000-1015_160.7\tDS:ADNI_1380^LP6005117-DNA_G04^ADNI_WGS\tLB:LP6005117-DNA_G04\tPL:illumina\tPU:HS2000-1015_160.7\tSM:ADNI_1380"
I want to extract everything after ID: and before first the \t
and get HS2000-1015_160.7 as a result. I would like a one-liner if possible.
code I tried:
echo ${RG} | grep -oP "(?<=ID:)[^"\t"]*"
which gives me HS2000-1015_160.7\
Try:
sed 's/.*ID://;s/\\t.*//'
s/.*ID:// remove everything in front and including ID:
s/\\t.*//' remove everything after and including \t characters.

storing result of awk in shell script [duplicate]

This question already has answers here:
How to split a string into an array in Bash?
(24 answers)
How do I split a string on a delimiter in Bash?
(37 answers)
Closed 3 years ago.
I am new to shell script and I have started it today itself. Can anyone tell me how to store and retrieve the output of this command:
cat /proc/cpuinfo | grep "MHz" | awk '/cpu MHz/ {print $4}'
This command returns just four decimal numbers but I am struggling to store these four numbers.
I have tried to store them in array but it has not been fruitful for me. Please help!!

Manipulate csv data using bash [duplicate]

This question already has answers here:
What's the most robust way to efficiently parse CSV using awk?
(6 answers)
Closed 3 years ago.
I have a CSV file with several rows and columns, some rows have 4 columns and some have 5 columns. I want to add to those with 4 columns, one more column so all the rows have 5 columns. The info that I must add must be in the 3rd row, or at the end.
The CSV file looks like this:
name;ip;supplier;os;manufact
How can this be done in bash?
You can use read to split the CSV, then output the desired number of columns.
cat test.csv | while read line; do
IFS=\; read -ra COL <<< "$line"
echo "${COL[0]};${COL[1]};${COL[2]};${COL[3]};${COL[4]};"
done
For example, with test.csv containing:
1;2;3;4
1;2;3;4;5
1;2;3;4;
1;2;3;4;5;
The above script outputs:
1;2;3;4;;
1;2;3;4;5;
1;2;3;4;;
1;2;3;4;5;

Resources