Convert text to columns


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Convert text to columns
# 1  
Old 10-16-2018
Convert text to columns

Hi.
I need an input file of a single word pr. line converted in into a list of random pairs.

i need input like this
Code:
word1
word2
word3
word4

to be outputted like this:
Code:
word3 word1
word2 word4

My attempt is a tedious while loop like this:

Code:
nfiles=$(cat inputfile | wc -l)       #'wc -l inputfile' outputs name of file. this is unwanted. 
while [[ $nfiles -gt 0 ]]; do
    field1=$(shuf -n 1 inputfile)
    sed -i /$field1/d inputfile
    field2=$(shuf -n 1 inputfile)
    sed -i /$field2/d inputfile 
    echo "$field1 $field2" >> outputfile 
    nfiles=$(cat inputfile | wc -l)
done

Any help much appreciated. Thanks

Last edited by BotHead; 10-16-2018 at 11:53 AM..
# 2  
Old 10-16-2018
Since you have shuf,
Code:
shuf inputfile > /tmp/$$
paste inputfile /tmp/$$ > outputfile
rm -f /tmp/$$

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-16-2018
Try also
Code:
shuf inputfile | paste -sd"\t\n"
word4    word1
word3    word2

These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 10-16-2018
Thank you. I wasn't aware of paste..
# 5  
Old 10-17-2018
Code:
shuf inputfile | paste - -

This User Gave Thanks to rdrtx1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert rows to columns

hi folks, I have a sample data like what is shown below: 1,ID=1000 1,Org=CedarparkHospital 1,cn=john 1,sn=doe 1,uid=User001 2,uid=User002 2,ID=2000 2,cn=steve 2,sn=jobs 2,Org=Providence I would like to convert it into the below format: 1,1000,CedarparkHospital,john,doe,User001... (11 Replies)
Discussion started by: vskr72
11 Replies

2. Shell Programming and Scripting

How to concatenate 2-columns by 2 -columns for a text file?

Hello, I want to concatenate 2-columns by 2-columns separated by colon. How can I do so? For example, I have a text file containing 6 columns separated by tab. I want to concatenate column 1 and 2; column 3 and 4; column 5 and 6, respectively, and put a colon in between. input file: 1 0 0 1... (10 Replies)
Discussion started by: huiyee1
10 Replies

3. Shell Programming and Scripting

Convert rows to columns

I am looking to print the data in columns and after every 3 words it should be a new row. cat example.out | awk 'END { for (i = 0; ++i < m;) print _;print _ }{ _ = _ x ? _ OFS $1 : $1}' m=1| grep -i INNER I am looking to print in a new line after every 3 words. ... (2 Replies)
Discussion started by: lazydev
2 Replies

4. Shell Programming and Scripting

How to Convert rows in to columns?

Hi Gurus, How to convert rows in to columns using linux shell scripting Input is like (sample.txt) ABC DEF GHI JKL MNO PQR STU VWX YZA BCD output should be (sampleoutput.csv) ABC,DEF,GHI,JKL,MNO PQR,STU,VWX,YZA,BCD (2 Replies)
Discussion started by: infasriniit
2 Replies

5. Shell Programming and Scripting

Convert few columns to rows

Hi! Does anybody help me in converting following data: INPUT looks like this: 20. 100. 30 200. 40. 400. 50. 100. 60. 200. 70. 400. 80. 200. 150. 210. 30. 100. OUTPUT should look like this: 20. 100. 30 200. 40. 400. 50. 100. 60. 200. 70.... (5 Replies)
Discussion started by: lovelinux
5 Replies

6. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

7. Shell Programming and Scripting

convert rows to columns

hi, i have the file as below: abc def ghi jkl i want the output as abc,def,ghi,jki please reply, Thanks (4 Replies)
Discussion started by: namitai
4 Replies

8. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies

9. Shell Programming and Scripting

how to convert fields from a text file to excel columns

i have this file which has the following contents: ,-0.3000 ,-0.3000 ,-0.3000 ,-0.9000 ,-0.9000 ,-0.9000 i would like to get this: -0.3-0.9-0.3-0.9-0.3-0.9 so far i am trying: awk '{for(i=1; i<=NF; i++) {printf("%f\n",$i)}}' test1 > test2 any help... (4 Replies)
Discussion started by: npatwardhan
4 Replies

10. Shell Programming and Scripting

how to convert columns to rows

Hi, I need a shell script for below requirement Input file P1 - 173310 P2 - 173476 P3 - 173230 P4 - 172737 P1 - 173546 P2 - 173765 P3 - 173876 P4 - 172989 Out put file P1 173310 173546 P2 173476 173765 P3 173230 173876 P4 172737 172989 Suresh (6 Replies)
Discussion started by: suresh3566
6 Replies
Login or Register to Ask a Question