Linux - Get Text from Second Tab

Linux - get text from second tab

awk:

awk '{print $2}' file.txt

cut:

cut -f2 file.txt

bash:

while read -a A; do echo ${A[1]}; done < file.txt

perl:

perl -lane 'print $F[1]' file.txt

If you know the string you are grepping for, you can use grep:

grep -o 'sometext12' file.txt

How to grep different strings followed by a tab on multiple lines

If you want everything past the first tab removed including the tab, this sed would do that sed 's/\t.*//g'

Alternatively, sed 's/\([^\t]*\)\t.*/\1/g' finds any non-tab character repeated any number of times, followed by a tab and any number of characters, captures the bit up to the first tab, and spits that out.

awk handles tab delimited input very well too. awk -F'\t' '!a[$1]++ {print}' will print out the deduped first field (delimited by tabs) for each line. This works by inserting and incrementing the value of an array keyed off the first field, so the first time it's encountered it evaluates to !0, so print is fired, and each subsequent time a value is seen it will be !1, !2, etc, evaluating to false and not printing.

Select first two columns from tab-delimited text file and and substitute with '_' character

Could you please try following.

awk '{print $2"_"$3}' Input_file

2nd solution:

awk 'BEGIN{OFS="_"} {print $2,$3}' Input_file

3rd solution: Adding a sed solution.

sed -E 's/[^ ]* +([^ ]*) +([^ ]*).*/\1_\2/' Input_file

How can I get 2nd and third column in tab delim file in bash?

cut(1) was made expressly for this purpose:

cut -f 2-3 input.txt > output.txt

replace text between two tabs - sed

I would suggest using awk here:

awk 'BEGIN { FS = OFS = "\t" } { $2 = "sample" } 1' file

Set the input and output field separators to a tab and change the second field. The 1 at the end is always true, so awk does the default action, { print }.

How to copy second column from all the files in the directory and place them as columns in a new text file

#!/bin/bash

# Be sure the file suffix of the new file is not .txt
OUT=AllColumns.tsv
touch $OUT

for file in *.txt
do
paste $OUT <(awk -F\\t '{print $2}' $file) > $OUT.tmp
mv $OUT.tmp $OUT
done

One of many alternatives would be to use cut -f 2 instead of awk, but you flagged your question with awk.

Since your files are so regular, you could also skip the do loop, and use a command-line utility such as rs (reshape) or datamash.



Related Topics



Leave a reply



Submit