awk and sed in bash script and no output
2
0
Entering edit mode
5.4 years ago
bk11 ★ 2.3k

Hi

I wrote a bash script with awk and sed commands in it to parse text files in a directory. This script runs without giving any output. What is wrong with it?

#!/bin/bash

for i in *.txt; do
       awk -F'\t' '{print $16}' $i | sed '1d' | tr -d '\r' >$i_get_signals.sh
done
bash sed awk • 2.9k views
ADD COMMENT
1
Entering edit mode

We need more information. What does your input data look like? What output do you expect? Are you running the script while you're in the directory that the .txt files are in? How are you invoking the script?

You will also need to tell us why this is a bioinformatics question and not just a programming question else we may close it as off topic.

ADD REPLY
0
Entering edit mode

Yes I am running the script in the same directory that has the text files. This is related to bioinformatics as it is used to get URL to download histone proteins binding signals from different cell types.

ADD REPLY
0
Entering edit mode

It is related to bioinformatics, but only borderline. It is essentially just unix text processing, and there are more specialised forums for this. We usually tolerate posts like this as we understand that these are common problems in bioinformatics, but it's not pure bioinformatics.

ADD REPLY
0
Entering edit mode

Why do you send the result to a .sh file?

ADD REPLY
0
Entering edit mode

I tried sending the result to .txt file but also it did not work.

ADD REPLY
0
Entering edit mode

It shouldn't make a difference, but with the extension .sh you suggest this is a shell script.

ADD REPLY
0
Entering edit mode

That is because OP is using *.txt in loop and doesn't want output with .txt for obvious reasons. Instead OP could have used any other extension instead of sh (.out, .text, .file etc or without extension). WouterDeCoster

ADD REPLY
0
Entering edit mode

Yeah, of course, and while file extensions have almost no meaning in unix it is quite confusing to use something unexpected.

ADD REPLY
2
Entering edit mode
5.4 years ago

try this:

for i in *.txt; do awk -F'\t' '{print $16}' $i | sed '1d' | tr -d '\r' > $i"_get_signals.sh" ; done

But whatever you are doing, this is long way of doing it. OP code is simply extracting 16th column, removing the header and removing carriage returns and writing it to a text file. This can be done better way.

Change the code to remove old file extension in new files:

for i in *.txt; do awk -F'\t' '{print $16}' $i | sed '1d' | tr -d '\r' > ${i%%.*}"_get_signals.sh" ; done
ADD COMMENT
0
Entering edit mode

It worked very nicely though.

ADD REPLY
0
Entering edit mode

@ bk11

  1. Make a new directory
  2. Copy all the files in the new directory
  3. Run following code:

    awk 'FNR!=1 {print $16 > FILENAME".out"}' *.txt

  4. Compare the output from above bash script.

ADD REPLY

Login before adding your answer.

Traffic: 2676 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6