Visit Our UNIX and Linux User Community


Concatenate text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate text file
# 1  
Old 10-27-2014
Concatenate text file

I have a text file in the below format:

Code:
chr1	10002681	10002826	LZIC
chr1	10002980	10003083	NMNAT1
chr1	10003485	10003573	NMNAT1
chr1	100111430	100111918	PALMD
chr1	100127874	100127955	PALMD
chr1	100133197	100133322	PALMD
chr1	100152231	100152346	PALMD
chr1	100152485	100152519	PALMD
chr1	100152631	100152745	PALMD
chr1	100154330	100155428	PALMD

Is it possible to concatenate $1":"$2"-"@3 in one column withe gene name next to that for each row?

Code:
 
chr1:10002681-10002826	LZIC
chr1:10002980-10003083	NMNAT1
chr1:10003485-10003573	NMNAT1
chr1:100111430-100111918	PALMD
chr1:100127874-100127955	PALMD
chr1:100133197-100133322	PALMD
chr1:100152231-100152346	PALMD
chr1:100152485-100152519	PALMD
chr1:100152631-100152745	PALMD
chr1:100154330-100155428	PALMD

Thanks Smilie.
# 2  
Old 10-27-2014
Code:
awk '{print $1 ":" $2 "-" $3 , $4}' file

# 3  
Old 10-27-2014
Code:
 awk '{print $1 ":" $2 "-" $3 , OFS= /t$4}' file

Would the above concatenate $1,$2,$3 in column 1 and $4 in column 2? Thanks Smilie.
# 4  
Old 10-27-2014
Quote:
Originally Posted by cmccabe
Code:
 awk '{print $1 ":" $2 "-" $3 , OFS= /t$4}' file

Would the above concatenate $1,$2,$3 in column 1 and $4 in column 2? Thanks Smilie.
Hello cmccabe,

Following will do the same what you have asked now, Akshay's solution is only printing the seprators in between fields.

Code:
awk '{$1=$1":"$2"-"$3;$2=$NF;$3=$NF="";print $0}'  Input_file

Thanks,
R. Singh
# 5  
Old 10-27-2014
Quote:
Originally Posted by cmccabe
Code:
 awk '{print $1 ":" $2 "-" $3 , OFS= /t$4}' file

Would the above concatenate $1,$2,$3 in column 1 and $4 in column 2? Thanks Smilie.
Your syntax is wrong, or else try like this

Code:
awk 'NF{$1=sprintf("%s:%s-%s",$1,$2,$3); $2=$4; NF-=2}1'  file

---------- Post updated at 10:33 PM ---------- Previous update was at 10:30 PM ----------

--
Quote:
Originally Posted by RavinderSingh13
Hello cmccabe,

Following will do the same what you have asked now, Akshay's solution is only printing the seprators in between fields.

Code:
awk '{$1=$1":"$2"-"$3;$2=$NF;$3=$NF="";print $0}'  Input_file

Thanks,
R. Singh
@Ravinder : $3=$NF="" will not delete fields actually, try your command with OFS=','
# 6  
Old 10-27-2014
Quote:
Originally Posted by cmccabe
. . .Would the above concatenate $1,$2,$3 in column 1 and $4 in column 2? . . .
What you call columns is called fields in awk etc. terms which in turn are separated by field separators (FS). So what will be interpreted as a field depends on the definition of the FS. It defaults to whitespace (space, tab, newline) in awk. With the defaults, your input will have four fields. Should FS be set to e.g. "#" or "," , your input will have just one single field.

So, the answer to your above question is "yes" if default FS are used, but might be "possible" or "no" if you define different FS.
# 7  
Old 10-27-2014
Code:
 awk 'NF{$1=sprintf("%s:%s-%s",$1,$2,$3); $2=$4; NF-=2}1'  file

Code:
chr1:10002681-10002826 LZIC
chr1:10002980-10003083 NMNAT1
chr1:10003485-10003573 NMNAT1


Seems to be printing a continuous string of text. Thanks Smilie.

Previous Thread | Next Thread
Test Your Knowledge in Computers #471
Difficulty: Medium
Google recommends that users configure Google Public NTP together with non-leap-smearing NTP servers.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 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. Programming

Concatenate string from text file

Hi mY files paths are defined as : //sbase = 'D:\data\sample_AMC\fasta_files\'; sbase2 = 'D:\data\sample_AMC\fasta_files\results\'; snameprefix = 'orig_ind'; snameprefix3 = 'results_ind'; ... const string filname = sbase + snameprefix + snamesuffix; const string resultsname_ =... (2 Replies)
Discussion started by: siya@
2 Replies

4. Shell Programming and Scripting

Concatenate text between patterns in individual strings

In any given file, wherever a certain data block exists I need to concatenate the values(text after each "=" sign) from that block. in that block. The block starts and ends with specific pattern, say BEGIN DS and END DS respectively. The block size may vary. A file will have multiple such blocks.... (12 Replies)
Discussion started by: Prev
12 Replies

5. Shell Programming and Scripting

Concatenate strings retrieved from a file and write it into another file

Hi, I have a file files.txt containing data as below: abc;xyz uvw;pqr 123;456 I want to develop strings like below using the above data and write them into another file: www/xxx/abc/yyy/xyz www/xxx/uvw/yyy/pqr www/xxx/123/yyy/456 All this needs to be done through .sh file. ... (4 Replies)
Discussion started by: archana.n
4 Replies

6. Shell Programming and Scripting

How to concatenate text files together

Hi. I'm attempting to copy 4 text files together to create one larger file. All four files contain text in the same format. Can I do this? I attempted this, but it didn't work: cp wscreening_test_h_po.dat+wscreening_test_b_po.dat+wscreening_test_h_notpo.dat... (1 Reply)
Discussion started by: buechler66
1 Replies

7. Shell Programming and Scripting

Insert file names when concatenate files into a file

Hi I found the following line would concatenate all test_01 test_02 test_03 files into "bigfile". cat test_* >> bigfile But, what I'm looking for a way to insert each file names in order when concatenated in "bigfile". Thank you samky2005 (2 Replies)
Discussion started by: samky2005
2 Replies

8. UNIX for Advanced & Expert Users

Concatenate text of two files in UNIX.

I have a file MyTest.csv saved in Unicode format in Incoming directory, and I have another file called MyTest.csv saved in ANSII format in InProcess Directory. I need to concatenate the text of both these files and put in another file MyTest.csv which is placed in the root directory. How do I... (1 Reply)
Discussion started by: Uniq
1 Replies

9. UNIX for Dummies Questions & Answers

file Concatenate

Hi All, I have a question about file concatenate on unix. I have two file 1 of them like aaa,bbb,ccc,ddd other one is eee,fff,ggg,hhh I want to concatenate those file like this position aaa,bbb,ccc,ddd,eee,fff,ggg,hhh how can I do this ?? thanks. Alice (3 Replies)
Discussion started by: alisev
3 Replies

Featured Tech Videos