'Append lines separated by comma while preserving the existing new line

Bash script used:

#!/bin/bash
set -xv
IS=$'\n'
list=$(cat exlist_sample | xargs -n1)
for  i in $list; do
    echo "$i" | rev > slist
    echo "$i" >> znamelist

    for x in $(cat slist);do
       echo "this is $x" >> znamelist
       echo $IS >> znamelist
    done
done

Input file used (exlist_sample)

dz-eggg-123
dz-fggg-123
lk-opipo-123
poipo-123-oiu

Current output (final_list)

dz-eggg-123
this is 321-ggge-zd

dz-fggg-123
this is 321-gggf-zd

lk-opipo-123
this is 321-opipo-kl

poipo-123-oiu
this is uio-321-opiop

Expected output:

dz-eggg-123,this is 321-ggge-zd

dz-fggg-123,this is 321-gggf-zd

lk-opipo-123,this is 321-opipo-kl

poipo-123-oiu,this is uio-321-opiop

How to achieve the expected output to make it in csv format in the sciprt while preserving the new line.



Solution 1:[1]

Here is my version of your script:

#!/bin/bash

inputfile="exlist_sample"
if [[ ! -f "$inputfile" ]]
then
    echo "ERROR: input file $inputfile not found."
    exit 1
fi
outputfile="znamelist"

while IFS= read -r line
do
    reverseline=$(echo "$line"| rev)
    echo -e "$line,this is $reverseline\n"
done < "$inputfile" >"$outputfile"
  • using while with read this way ensures the script will work ok even if there are spaces in lines. It might be overkill a bit for your specific requirement here, but better learn the "safe" way to do it.

  • no need to use files to store the reversed line, you can store it in a variable in each while iteration.

    $ cat znamelist 
    dz-eggg-123,this is 321-ggge-zd
    
    dz-fggg-123,this is 321-gggf-zd
    
    lk-opipo- 123,this is 321 -opipo-kl
    
    poipo-123-oiu,this is uio-321-opiop
    

Solution 2:[2]

Would you please try the following:

#!/bin/bash

while IFS= read -r i; do                        # read the input file line by line
    j=$(rev <<< "$i")                           # reverse the string
    printf "%s,this is %s\n" "$i" "$j"          # print the original string and the reversed one
done < exlist_sample > znamelist

Output:

dz-eggg-123,this is 321-ggge-zd
dz-fggg-123,this is 321-gggf-zd
lk-opipo-123,this is 321-opipo-kl
poipo-123-oiu,this is uio-321-opiop

Solution 3:[3]

A one-liner using paste, sed, and rev (though not a POSIX utility) utilities and bash process substitution could be:

paste -d, exlist_sample <(rev exlist_sample | sed 's/^/this is /') > znamelist

Solution 4:[4]

Using any awk in any shell on every Unix box pretty efficiently and using almost no memory:

$ awk -v OFS=',' -v ORS='\n\n' '{r=""; for (i=1;i<=length();i++) r=substr($0,i,1) r; print $0, "this is " r}' file
dz-eggg-123,this is 321-ggge-zd

dz-fggg-123,this is 321-gggf-zd

lk-opipo-123,this is 321-opipo-kl

poipo-123-oiu,this is uio-321-opiop

or using more memory but more efficiently:

$ rev file | awk -v OFS=',' -v ORS='\n\n' 'NR==FNR{r[NR]=$0; next} {print $0, "this is " r[FNR]}' - file
dz-eggg-123,this is 321-ggge-zd

dz-fggg-123,this is 321-gggf-zd

lk-opipo-123,this is 321-opipo-kl

poipo-123-oiu,this is uio-321-opiop

or [probably] most efficiently using almost no memory:

$ rev file | awk -v OFS=',' -v ORS='\n\n' '{r=$0} (getline < "file") > 0{print $0, "this is " r}'
dz-eggg-123,this is 321-ggge-zd

dz-fggg-123,this is 321-gggf-zd

lk-opipo-123,this is 321-opipo-kl

poipo-123-oiu,this is uio-321-opiop

Make sure to read and understand awk.freeshell.org/AllAboutGetline if you're going to use that last one. Personally I wouldn't unless memory and efficiency are both issues.

Solution 5:[5]

Solution

rev input.txt | sed 's/^/this is /' | paste -d, input.txt - | sed G

Input

? cat input.txt 
dz-eggg-123
dz-fggg-123
lk-opipo-123
poipo-123-oiu

Output

dz-eggg-123,this is 321-ggge-zd

dz-fggg-123,this is 321-gggf-zd

lk-opipo-123,this is 321-opipo-kl

poipo-123-oiu,this is uio-321-opiop

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nic3500
Solution 2 tshiono
Solution 3 M. Nejat Aydin
Solution 4
Solution 5 Weihang Jian