tab seperated textfile and write each field in a variable
Hello,
I have a textfile with tab separate content. The goal is, to insert fiels3 in some lines. The first line is without field3. filed6 contains a space, which makes the trouble Code:
F1 F2 F4 F5 F 6 Code:
#!/bin/bash Code:
Any idea to this? Thanks Midyr |
you do not need to use awk at all:
https://github.com/koalaman/shellcheck/wiki/SC2206 Code:
xy=($(awk -F '\t' '{print $1, $2, $3, $4, $5, $6}' )) did you check xy[6] ? |
But if you do use AWK, keep in mind that the Field Separator (FS) and the Output Field Separator (OFS) are different variables.
Code:
xy=($(awk 'BEGIN {FS="\t"; OFS="\t"} {print $1, $2, $3, $4, $5, $6}' )) |
Quote:
|
As the formatting wasn't saved for me on pasting, may I ask what is at field 3 in the line where it is missing?
I have assumed 2 spaces to make up for the missing field, so the below shows how bash can handle this: Code:
!/usr/bin/env bash Code:
$ ./script.sh |
Quote:
|
Quote:
Code:
awk -F\\t -vOFS=\\t '1,$3!=""&&$3="NEW"' index.txt |
Quote:
Thanks, that's, what i'm locking for! |
Does it print all lines?
The following has an unconditional and explicit print Code:
awk 'BEGIN { FS=OFS="\t" } $3=="" { $3="NEW" } { print }' < index.txt With bash builtins: have a while read loop over the lines. There is no good function to print with embedded delimiters, so here is one. Code:
#!/bin/bash And another variant: Code:
#!/bin/bash |
@MadeInGermany. Two problems with your Bash code:
1) read counts consecutive runs of an IFS character as one delimiter. Probably not was the OP wanted. 2) \n in printf "$fs%s\n" "$@" is unnecessary. And a small enhancement: Code:
out=$(printf "$fs%s" "${fields[@]}") Code:
printf -v out "$fs%s" "${fields[@]}" |
or probably
Code:
printf -v out "$fs%s" "${fields[*]}" |
All times are GMT -5. The time now is 06:18 PM. |