Question: awk and sed in bash script and no output
0
gravatar for bk11
2.0 years ago by
bk1140
bk1140 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 • 1.1k views
ADD COMMENTlink modified 2.0 years ago by Alex Reynolds31k • written 2.0 years ago by bk1140
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 2.0 years ago • written 2.0 years ago by Joe18k

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 2.0 years ago by bk1140

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 2.0 years ago by WouterDeCoster44k

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

ADD REPLYlink written 2.0 years ago by WouterDeCoster44k

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

ADD REPLYlink written 2.0 years ago by bk1140

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

ADD REPLYlink written 2.0 years ago by WouterDeCoster44k

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 2.0 years ago by cpad011214k

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

ADD REPLYlink written 2.0 years ago by WouterDeCoster44k
2
gravatar for cpad0112
2.0 years ago by
cpad011214k
Hyderabad India
cpad011214k 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 2.0 years ago • written 2.0 years ago by cpad011214k

It worked very nicely though.

ADD REPLYlink written 2.0 years ago by bk1140

@ 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 2.0 years ago • written 2.0 years ago by cpad011214k
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: 1974 users visited in the last hour