LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   output lines in place of columns (https://www.linuxquestions.org/questions/linux-newbie-8/output-lines-in-place-of-columns-4175699877/)

Drosera_capensis 08-29-2021 12:05 PM

output lines in place of columns
 
Hello everyone,

I want to output data in row rather than in column after having selected a pattern from a list with awk. But the command output truncated data,

The original file:

Code:

head -3 file.txt
Scaffolds_16_pilon      ACmerged_contig_806    5_1009.75816
Scaffolds_16_pilon      ACmerged_contig_806    11_1010
Scaffolds_16_pilon      ACmerged_contig_806    11_1011

An attempt to output the data in row using a loop with list of the header:

Code:

cat  list.txt | while read line ;
do 
        awk /"$line"/'{print $3}' file.txt | tr "\n" "\t" ;
        echo " " ;
done

Code:

5_1009.7 1_1011
It seems that the outputs are mixed up and truncated.
Would anyone know how to avoid it?

RandomTroll 08-29-2021 07:59 PM

Code:

cat list.txt | cut -f3 | tr '\n' '\t'
?

JJJCR 08-29-2021 09:14 PM

Try this:

awk '{ print $3 "\n" }' < file.txt

allend 08-30-2021 01:03 AM

From https://www.gnu.org/software/gawk/ma...hell-Variables
Code:

printf "Enter search pattern: "
read pattern
awk "/$pattern/ "'{printf $3"\t"};END {printf "\n"}' file.txt

or, better,
Code:

printf "Enter search pattern: "
read pattern
awk -v pat="$pattern" '$0 ~ pat {printf $3"\t"};END {printf "\n"}' file.txt


MadeInGermany 08-30-2021 03:33 AM

Make awk remove a WinDOS CR (also represented as ^m or \r).
Code:

while read line
do
        awk '{sub(/\r$/,"")}'"/$line/"'{out=(out dl $3); dl="\t"} END {print out}' file.txt
done < list.txt


pan64 08-30-2021 03:43 AM

what is in list.txt ?

Drosera_capensis 08-30-2021 12:23 PM

My mistake not to have indicated the content of list.txt pan64. They were the headers of the file.txt.

Thank you Allend and MadeInGermany. These version of the script work perfectly.
I am not familiar with some of the syntax you have used for this answer. I will have a closer look to your script.

pan64 08-30-2021 01:05 PM

so maybe:
Code:

grep -f list.txt file.txt | awk '{ print $3 }'
how do you want to handle duplicated headers?

allend 08-30-2021 07:53 PM

Following the idea from MadeInGermany, I would like to revise my suggestion.
Code:

awk -v pat="$pattern" '$0 ~ pat {printf dl$3; dl="\t"};END {printf "\n"}' file.txt
This exploits that the dl (for delimiter) variable is initially unset, so is not output on the first match, but is then output for each subsequent match. This avoids the unnecessary tab character at the end of the line that my initial suggestion produces.

Drosera_capensis 08-31-2021 03:31 AM

Thanks a lot pan64 and allend!

The subject has been marked as solved, but actually, it is not. I think there is a problem with the format of my dataset rather than the scripts that have been posted.
I have tried with your last command allend, and it does not work either. I guess I have to spend more time working with awk...again!

I wish as well to understand this simple command I was using routinely does not work in this case. It is puzzling.

Quote:

| tr "\n" "\t"

pan64 08-31-2021 03:44 AM

would be nice to explain exactly what did you try, what did not work (and how), and what do you wish exactly.
Probably a better example can help too.

MadeInGermany 08-31-2021 04:05 AM

Attention: a variable having an unknown value should not appear in the format string (first arg of printf or sprintf); the characters % and \ are interpreted in a special way.
Unsafe:
Code:

printf dl$3
Safe:
Code:

printf "%s%s", dl, $3
Also safe here (dl is known):
Code:

printf (dl "%s"), $3

Drosera_capensis 08-31-2021 06:17 AM

Thanks much for your patience.

I will take another example on this set of data, which illustrate better the problem.

I have files like this one:

Quote:

cat file.txt

Becon104Scf14253g0001.1
6_538.8822208
6_463.0917901
6_44.74786012
And I wish to output:
Quote:

Becon104Scf14253g0001.1 6_538.8822208 6_463.0917901 6_44.74786012
But when I am trying the following command, it outputs an odd visual error (even on different machines):

Quote:

cat file.txt | tr "\n" "\t"
Would anyone have an idea why it is the case?

pan64 08-31-2021 06:22 AM

what kind of visual errors ?
Code:

grep -f list.txt file.txt | awk '{ printf $3"\t" }'

Drosera_capensis 08-31-2021 06:32 AM

The characters "Becon104" are printed in the left corner of the terminal, just before my usr:/path identifier. And there is no output.

It is almost like the output are printed, but covered by my usr:/path.


All times are GMT -5. The time now is 05:12 AM.