Home > Mobile >  To split the output(s) of a script into two fields and insert that output from a specific row in a c
To split the output(s) of a script into two fields and insert that output from a specific row in a c

Time:11-11

I am trying to split the output of the following code into two fields and insert it from the 3rd row of a csv file

#!/bin/bash
cid=`git log -n 1 --pretty=format:"%H"`
git diff-tree --no-commit-id --name-only -r $cid | xargs -I {} echo '\'{} | xargs -I {} md5sum > final.csv

Current Output comes as a single line ( need to be separated into fields)

title,Path          
              
l34sdg232f00b434532196298ecf8427e /path/to/file              
sg35s3456f00b204e98324998ecsdf3af /path/to/file                 

Expected Output

final.csv

title,Path                     

l34sdg232f00b434532196298ecf8427e,/path/to/file            
sg35s3456f00b204e98324998ecsdf3af,/path/to/file            

I am thinking of placing the output of the script in a third file and then reading that file line by line using awk. Not sure if that's the correct way to proceed. Thanks in advance.

CodePudding user response:

You seem to be overcomplicating things.

#!/bin/sh
cid=$(git log -n 1 --pretty=format:"%H")
git diff-tree --no-commit-id --name-only -r "$cid" |
xargs md5sum |
sed 's/  /,/' > final.csv

This simply replaces the two spaces in the md5sum output with a comma.

Because nothing here is Bash-specific, I changed the shebang to #!/bin/sh; obviously, still feel free to use Bash if you prefer.

I also switched from the obsolescent `backtick` syntax to modern $(command substitution) syntax.

If you absolutely require the CSV header on top, adding that in the sed script should be trivial. Generally, header lines are more of a nuisance than actually useful, so maybe don't.

CodePudding user response:

This kind of does what you're asking:

#!/bin/bash
cid=$(git log -n 1 --pretty=format:"%H")
git diff-tree --no-commit-id --name-only -r "$cid" | while read -r path
do
        md5sum "${path}"
done | awk 'BEGIN{printf "%s,%s\n", "title", "path";printf "\n"}{printf "%s,%s\n",$1,$2}' > final.csv
  • Related