Separate fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Separate fields
# 1  
Old 10-28-2014
Separate fields

Code:
 awk 'NF==2{s=$1;next}{$(NF+1)=s}1' sort.txt > output.txt

Code:
A_16_P32713632 chr10 90695750 90695810 ACTA2
A_16_P32713635 chr10 90696573 90696633 ACTA2
A_16_P32713680 chr10 90697419 90697479 ACTA2

The command above outputs the data as a string separated by a space in 1 field. I can not seem to figure out how to output the data in 5 separate fields? Thanks Smilie.
# 2  
Old 10-28-2014
Please explain your problem, I guess this post might be a part of your last post Name in lats column of row
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 10-28-2014
It is part of that post, but I figured since it was a modification of that it might be better to start a new thread. Basically, as is the output is one line with the text separated by a space.

So the below is all one 1 line in 1 field.

Code:
 A_16_P32713632 chr10 90695750 90695810 ACTA2

Can each text be in the line be 5 separate fields? Thanks Smilie.
# 4  
Old 10-28-2014
How are you defining a "field"? If space is your separator, then that is five fields.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-28-2014
Field is a column (thinking in excel terms).

So,

Code:
A                         B         C              D              E
A_16_P32713632    chr10   90695750   90695810   ACTA2

Thanks Smilie.
# 6  
Old 10-28-2014
Right. So, if spaces are your separator, you have five fields.

You mention excel, so perhaps your problem is "I want to open this in excel but it looks weird". That would have been nice to know. This is what Excel expects for columns and rows in a text flatfile:

Code:
awk -v OFS="\t" -v ORS="\r\n" 'NF==2{s=$1;next}{$(NF+1)=s}1' sort.txt > output.txt

Beware that Excel is infamous for mangling data as it sees fit, especially anything that can be mistaken for a date.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 10-28-2014
Thank you very much Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Splitting a column in two separate fields

for making a summary I have a CSV file which is transformed to .DAT. I have an AWK file which is supposing to do the mapping of the DAT file. The code from the AWK file is the one below. The content of the DAT file looks like this (tab separated): ODT AGE CDT CO SEX TIME VALUE COMMENT ... (1 Reply)
Discussion started by: grikoss
1 Replies

4. UNIX for Dummies Questions & Answers

using sed delete a line from csv file based on specific data in two separate fields

Hello, :wall: I have a 12 column csv file. I wish to delete the entire line if column 7 = hello and column 12 = goodbye. I have tried everything that I can find in all of my ref books. I know this does not work /^*,*,*,*,*,*,"hello",*,*,*,*,"goodbye"/d Any ideas? Thanks Please... (2 Replies)
Discussion started by: Chris Eagleson
2 Replies

5. Shell Programming and Scripting

awk: Print fields between two delimiters on separate lines and send to variables

I have email headers that look like the following. In the end I would like to accomplish sending each email address to its own variable, such as: user1@domain.com='user1@domain.com' user2@domain.com='user2@domain.com' user3@domain.com='user3@domain.com' etc... I know the sed to get rid of... (11 Replies)
Discussion started by: tay9000
11 Replies

6. Shell Programming and Scripting

separate the file according to the number of fields

I have a file which is delimetered by ',' i need to filter out a file with respect to the number of fileds in each line. a,s,d,f,g,h,j,k,l 1,2,3,3,4,5,6,7,6 a,2,3 4,5,6,7 in this i neeed to filter out the lines with 8 column to another file and rest to another file. so ... (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

7. Shell Programming and Scripting

How to create a CSV File by reading fields from separate files

SHELL SCRIPT Hi, I have 3 separate files within a folder. Every File contains data in a single column like File1 contains data mayank sushant dheeraj File2 contains DSA_AT MG_AT FLAT_09 File3 contains data 123123 232323 (2 Replies)
Discussion started by: mayanksargoch
2 Replies

8. Shell Programming and Scripting

Separate fields

Hi everyone! I have a field like that: I need to keep I don't know how to use the Capital character like a separator and how to keep only this one... I guess sed could do something like that... Thanks;) (3 Replies)
Discussion started by: Castelior
3 Replies

9. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

10. UNIX for Dummies Questions & Answers

Separate special character fields

Hi, I have a file with User ID and User Name. Sometimes the file has speical characters in the USer ID and it creates problems. I want to allow all those fields to be processed which have only numbers and characters. I do NOT want to process those fields with Speical Characters. How... (1 Reply)
Discussion started by: baanprog
1 Replies
Login or Register to Ask a Question