Visit The New, Modern Unix Linux Community


awk to parse current and next row in tab-delimited file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk to parse current and next row in tab-delimited file
# 1  
awk to parse current and next row in tab-delimited file

Hi there,

I would like to use awk to reformat a tab-delimited file containing three columns as follows:
Data file:
Code:
sample    1    173
sample    269    530
sample    687    733
sample    1699 1779

Desired output file:

Code:
sample 174..265, 531..686, 734..1698

I need the value in the third column +1 to be paired with the next value in the second column -1, and so on. The resultant output would be a single line containing the name of the first column followed by a space and then the parsed paired coordinates.

Thank you very much for your help!

Last edited by emiley; 07-26-2016 at 06:05 PM.. Reason: code not appearing correctly
# 2  
What have you tried so far? Are you getting errors? Please post the awk commands and any output.
# 3  
I have tried this (among a variety of other variations), but clearly far from where I want to be.... Very much a beginner...

Code:
awk '{print $1" "$3+1".."$2-1;}' input

With the following output:
Code:
sample 174..0
sample 531..268
sample 734..686
sample 1780..1698

Thank you for your help, I really appreciate it!
# 4  
Code:
awk -F"\t" '
NR==1 {printf $1 " " ; l=$NF + 1;}
NR>2 {printf ", "}
NR>1 {printf l ".." $2 - 1; l=$NF + 1}
END {print ""}
' infile


Last edited by rdrtx1; 07-26-2016 at 09:03 PM..
# 5  
In the 2nd column of your 2nd line you have the number 269. In your output you have got 265, shouldn't this be 268?

The below is a pure shell solution:

Code:
f=0 ; while read -r one two three; do
  ((!$f)) && { printf "%s" "${one} $(($three+1)).."; f=1; continue;}
  printf "%s, %s.." "$(($two-1))" "$(($three+1))"
done < datafile; print

# 6  
Another awk approach if you have different first column records:-
Code:
awk -F'\t' '
        {
                ++R[$1]
                A2[$1 FS R[$1]] = $2
                A3[$1 FS R[$1]] = $3
        }
        END {
                for ( k in R )
                {
                        i = 1
                        printf "%s\t", k
                        while ( i < R[k] )
                        {
                                printf "%d..%d ", A3[k FS i]+1, A2[k FS (i+1)]-1
                                ++i
                        }
                        printf "\n"
                }
        }
' file


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #27
Difficulty: Medium
Linux was the first fully supported 64-bit operating system for consumers.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

3. Shell Programming and Scripting

How to make tab delimited file to space delimited?

Hi How to make tab delimited file to space delimited? in put file: ABC kgy jkh ghj ash kjl o/p file: ABC kgy jkh ghj ash kjl Use code tags, thanks. (1 Reply)
Discussion started by: jagdishrout
1 Replies

4. Shell Programming and Scripting

Parse tab delimited file, check condition and delete row

I am fairly new to programming and trying to resolve this problem. I have the file like this. CHROM POS REF ALT 10_sample.bam 11_sample.bam 12_sample.bam 13_sample.bam 14_sample.bam 15_sample.bam 16_sample.bam tg93 77 T C T T T T T tg93 79 ... (4 Replies)
Discussion started by: empyrean
4 Replies

5. Shell Programming and Scripting

Help with converting Pipe delimited file to Tab Delimited

I have a file which was pipe delimited, I need to make it tab delimited. I tried with sed but no use cat file | sed 's/|//t/g' The above command substituted "/t" not tab in the place of pipe. Sample file: abc|123|2012-01-30|2012-04-28|xyz have to convert to: abc 123... (6 Replies)
Discussion started by: karumudi7
6 Replies

6. UNIX for Dummies Questions & Answers

Using awk to log transform a column in a tab-delimited text file?

How do I use awk to log transform the fifth column of a tab-delimited text file? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

7. UNIX for Dummies Questions & Answers

Delete header row and reformat from tab delimited to fixed width

Hello gurus, I have a file in a tab delimited format and a header row. I need a code to delete the header in the file, and convert the file to a fixed width format, with all the columns aligned. Below is a sample of the file:... (4 Replies)
Discussion started by: chumsky
4 Replies

8. Shell Programming and Scripting

splitting tab-delimited file with awk

Hi all, I need help to split a tab-delimited list into separate files by the filename-field. The list is already sorted ascendingly by filename, an example list would look like this; filename001 word1 word2 filename001 word3 word4 filename002 word1 word2 filename002 word3 word4... (4 Replies)
Discussion started by: perkele
4 Replies

9. UNIX for Dummies Questions & Answers

Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?

Hey Everybody, I am having much trouble figuring this out, as I am not really a programmer..:mad: Datafile.txt Column0 Column1 Column2 ABC DEF xxxGHI I am running using WGET on a cronjob to grab a datafile, but I need to cut the first three characters from... (6 Replies)
Discussion started by: rickdini
6 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies

Featured Tech Videos