Question: awk and sed in bash script and no output
0
gravatar for bk11
28 days ago by
bk1130
bk1130 wrote:

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
awk sed bash • 231 views
ADD COMMENTlink modified 28 days ago by Alex Reynolds26k • written 28 days ago by bk1130
1

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 REPLYlink modified 28 days ago • written 28 days ago by jrj.healey9.1k

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 REPLYlink written 28 days ago by bk1130

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 REPLYlink written 28 days ago by WouterDeCoster35k

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

ADD REPLYlink written 28 days ago by WouterDeCoster35k

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

ADD REPLYlink written 28 days ago by bk1130

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

ADD REPLYlink written 28 days ago by WouterDeCoster35k

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 REPLYlink written 28 days ago by cpad011210k

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

ADD REPLYlink written 28 days ago by WouterDeCoster35k
2
gravatar for cpad0112
28 days ago by
cpad011210k
India
cpad011210k wrote:

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 COMMENTlink modified 28 days ago • written 28 days ago by cpad011210k

It worked very nicely though.

ADD REPLYlink written 28 days ago by bk1130

@ 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 REPLYlink modified 28 days ago • written 28 days ago by cpad011210k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1445 users visited in the last hour