简体   繁体   中英

Bash concatenate line from file w/ boolean

I've been trying to read in a file line by line and output each line with the value and boolean result.

Contents of the text file

082843ab-c1e5-4729-8c03-2cec11996f01
09180b12-21b3-4bdb-a27b-ef9c026909f3

Script used to parse the file

#!/bin/bash

my_file="${1}"

declare -a myarray
let i=0
while IFS=$'\n' read -r line_data; do
    myarray[i]="${line_data}"
    ((++i))
done < $my_file

force_transmission="true"
let i=0
while (( ${#myarray[@]} > i ));do
    new_var=${myarray[i++]}
    combined_var="$new_var $force_transmission"
    echo $combined_var

    #this will print out the external id(file)
    #printf "${myarray[i++]}\n"
done

Expectataion

082843ab-c1e5-4729-8c03-2cec11996f01 true
09180b12-21b3-4bdb-a27b-ef9c026909f3 true

Actual

true3ab-c1e5-4729-8c03-2cec11996f01
trueb12-21b3-4bdb-a27b-ef9c026909f3

Any help would be very much appreciated, i've tried like 5 variations of this

Your input file has DOS newlines. When printed, these send the cursor back to the beginning of the line, thus overwriting content afterwards.

Use dos2unix on your input file, or modify the line:

new_var=${new_var%$'\r'} # remove $'\r', a CR character, from the end of new_var

If that's what you want to do use one of the specialized tools instead of bash scripting

UPDATED to eliminate the CR , just

$ sed 's/\r$/ true/' file

082843ab-c1e5-4729-8c03-2cec11996f01 true
09180b12-21b3-4bdb-a27b-ef9c026909f3 true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM