LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-29-2021, 12:05 PM   #1
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Rep: Reputation: Disabled
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?

Last edited by Drosera_capensis; 08-29-2021 at 12:20 PM.
 
Old 08-29-2021, 07:59 PM   #2
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,953

Rep: Reputation: 270Reputation: 270Reputation: 270
Code:
cat list.txt | cut -f3 | tr '\n' '\t'
?
 
Old 08-29-2021, 09:14 PM   #3
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,141

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Try this:

awk '{ print $3 "\n" }' < file.txt
 
Old 08-30-2021, 01:03 AM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,357

Rep: Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739
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
 
1 members found this post helpful.
Old 08-30-2021, 03:33 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,767

Rep: Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192
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

Last edited by MadeInGermany; 08-30-2021 at 03:58 AM.
 
2 members found this post helpful.
Old 08-30-2021, 03:43 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,684

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
what is in list.txt ?
 
Old 08-30-2021, 12:23 PM   #7
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-30-2021, 01:05 PM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,684

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
so maybe:
Code:
grep -f list.txt file.txt | awk '{ print $3 }'
how do you want to handle duplicated headers?
 
1 members found this post helpful.
Old 08-30-2021, 07:53 PM   #9
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,357

Rep: Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739Reputation: 2739
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.
 
1 members found this post helpful.
Old 08-31-2021, 03:31 AM   #10
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Original Poster
Rep: Reputation: Disabled
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"
 
Old 08-31-2021, 03:44 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,684

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
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.
 
Old 08-31-2021, 04:05 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,767

Rep: Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192
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
 
Old 08-31-2021, 06:17 AM   #13
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Original Poster
Rep: Reputation: Disabled
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?
 
Old 08-31-2021, 06:22 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,684

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
what kind of visual errors ?
Code:
grep -f list.txt file.txt | awk '{ printf $3"\t" }'
 
Old 08-31-2021, 06:32 AM   #15
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply

Tags
awk regex, columns


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Converting a file with Rows and Columns to just Columns mphillips67 Linux - Newbie 14 03-05-2014 10:31 AM
[SOLVED] bash suggestions to convert horizontal columns to vertical columns Guyverix Programming 14 01-24-2013 11:03 AM
SQL statements howto -- 3 columns input but 2 columns output fhleung Programming 3 11-29-2012 10:45 AM
Map 1 CSV's columns to matching columns in another CSV 2legit2quit Programming 7 10-27-2011 08:53 AM
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 06:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration