Adding null/Zero values to comma delimeted file using unix scripting - linux

I have a requirment where, I get files from source with different number of delimeter data, i need to make them to one standard number of delimeted data.
source file1:
AA,BB,CC,0,0
AC,BD,DB,1,0
EE,ER,DR,0,0
What i want to do is appened an extra 3 zeros at the end for each row
AA,BB,CC,0,0,0,0,0
AC,BD,DB,1,0,0,0,0
EE,ER,DR,0,0,0,0,0
The source file always contains less number of column data . Can anyone help on this.
Thanks In Advance

Try this, it will add particular string after each line of mentioned file
sed '1,$ s/$/,0,0,0/' infile > outfile
Here is what I tried;

sed can do it in place with the -i flag
sed -i "s/$/,0,0,0/g" file

Related

linux shell script delimiter

How to change delimiter from current comma (,) to semicolon (;) inside .txt file using linux command?
Here is my ME_1384_DataWarehouse_*.txt file:
Data Warehouse,ME_1384,Budget for HW/SVC,13/05/2022,10,9999,13/05/2022,27,08,27,08
Data Warehouse,ME_1384,Budget for HW/SVC,09/05/2022,10,9999,09/05/2022,45,58,45,58
Data Warehouse,ME_1384,Budget for HW/SVC,25/05/2022,10,9999,25/05/2022,7,54,7,54
Data Warehouse,ME_1384,Budget for HW/SVC,25/05/2022,10,9999,25/05/2022,7,54,7,54
It is very important that value of last two columns is number with 2 decimal places, so value of last 2 columns in first row for example is:"27,08"
That could be the main problem why delimiter couldn't be change in proper way.
I tried with:
sed 's/,/;/g' ME_1384_DataWarehouse_*.txt
and every comma sign has been changed, including mentioned value of the last 2 columns.
Is there anyone who can help me out with this issue?
With sed you can replace the nth occurrence of a certain lookup string. Example:
$ sed 's/,/;/4' file
will replace the 4th comma with a semicolon.
So, if you know you have 11 fields (10 commas), you can do
$ sed 's/,/;/g;s/;/,/10;s/;/,/8' file
Example:
$ seq 1 11 | paste -sd, | sed 's/,/;/g;s/;/,/10;s/;/,/8'
1;2;3;4;5;6;7;8,9;10,11
Your question is somewhat unclear, but if you are trying to say "don't change the last comma, or the third-to-last one", a solution to that might be
perl -pi~ -e 's/,(?![^,]+(?:,[^,]+,[^,]+)?$)/;/g' ME_1384_DataWarehouse_*.txt
Perl in isolation does not perform any loop over the input lines, but the -p option says to loop over input one line at a time, like sed, and print every line (there is also -n to simulate the behavior of sed -n); the -i~ says to modify the file, but save the original with a tilde added to its file name as a backup; and the regex uses a negative lookahead (?!...) to protect the two fields you want to exempt from the replacement. Lookaheads are a modern regex feature which isn't supported by older tools like sed.
Once you are satisfied with the solution, you can remove the ~ after -i to disable the generation of backups.
You can do this with awk:
awk -F, 'BEGIN {OFS=";"} {a=$NF;NF-=1; printf "%s,%s\n",$0,a} ' input_file
This should work with most awk version (do not count on Solaris standard awk)
The idea is to store the last element from row in variable, decrease the number of fields and then print using new delimiter, comma and stored last field.

Is there a way to convert one column in csv file to upper using shell commands?

Please help me with a BASH code which targets a particular column in a csv file and converts it to upper.
For instance, if file_a.csv has the following columns:
man,woman,boy,girl
woman,man,boy,girl
boy,girl,man,woman
girl,boy,woman,man
I want to convert column 2 to upper in order to have:
man,WOMAN,boy,girl
woman,MAN,boy,girl
boy,GIRL,man,woman
girl,BOY,woman,man
Thanks for your help
You can accomplish this with sed:
sed 's/[^,]*/\U&/2' file_a.csv
This will replace the 2nd string with with zero or more non-comma characters with it's uppercase equivalent

How to change single column of a file to uppercase in linux?

I have a file with two fields. I need to change the first field values from lowercase to uppercase. Can anyone give me a suggestion on how can I do this?
sample file data
e6|VerizonOctoberWB_PromoE7E6
e2|VerizonOctoberWB_UnlimwP_E1E2
e5|VerizonOctoberWB_PromoLI_E5
In above sample data I need to change the first field values(e6,e2,e5)
Given your small and poorly formatted sample:
cat up
e6|VerizonOctoberWB_PromoE7E6
e2|VerizonOctoberWB_UnlimwP_E1E2
e5|VerizonOctoberWB_PromoLI_E5
sed -r 's/^([^|]+)/\U\1\E/g' up
E6|VerizonOctoberWB_PromoE7E6
E2|VerizonOctoberWB_UnlimwP_E1E2
E5|VerizonOctoberWB_PromoLI_E5
Edit 1: added explanation:
search for and remember everything from beginning of line up to the first separator |, replace with \U(start upper-casing), \1 remembered string, \E stop upper-casing.

Combine first two columns of a single csv file into another column

So I have a large CSV file (in Gb) where I have multiple columns, the first two columns are :
Invoice number|Line Item Number
I want a unix / linux /ubuntu command which can merge this two columns and create a new column which is separated by separator ':', so for eg : If invoice number is 64789544 and Line Item Number is 234533, then my Merged value should be
64789544:234533
Can it really be achieved, If yes can the merged column is possible to be added back to the source csv file.
You can use the following sed command:
$ cat large.csv
Invoice number|Line Item Number|Other1|Other2
64789544|234533|abc|134
64744123|232523|cde|awc
$ sed -i.bak 's/^\([^|]*\)|\([^|]*\)/\1:\2/' large.csv
$ cat large.csv
Invoice number:Line Item Number|Other1|Other2
64789544:234533|abc|134
64744123:232523|cde|awc
Just be aware that it will take a backup of your input file just in case so you need to have enough space in your file system.
Explanations:
s/^\([^|]*\)|\([^|]*\)/\1:\2/ this command will replace the first two field of your CSV separated by | and will replace the separator by : using back references what will merge the 2 columns.
If you are sure about what you are doing, you can change -i.bak in -i to avoid taking a backup of the CSV file.
Perhaps with this simple sed
sed 's/|/:/' infile

grep in two files returning two columns

I have a big file like this:
79597700
79000364
79002794
79002947
And other big file like this:
79597708|11
79000364|12
79002794|11
79002947|12
79002940|12
Then i need the numbers that appear in the second file that are in the first file bur with the second column, something like:
79000364|12
79002794|11
79002947|12
79002940|12
(The MSISDN that appear in the first file and appear in the second file, but i need return the two columns of the second file)
Who can help me, because whit a grep does not work to me because return only the MSISDN without the second column
and with a comm is not possible because each row is different in the files
Try this:
grep -f bigfile1 bigfile2
Using awk:
awk -F"|" 'FNR==NR{f[$0];next}($1 in f)' file file2
Source: return common fields in two files

Resources