Grep and cat combined


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep and cat combined
# 1  
Old 03-20-2013
Grep and cat combined

Hello,
i need to search one word (snp1) from many files and copy the content of the columns of this word in new file.
example:
file 1:
Code:
SNP   BP   CHR  P
snp1  1     3     0.01
snp2  2     2     0.05
.
.

file 2:
Code:
SNP   BP   CHR  P
snp1  1     3     0.06
snp2  2     2     0.3

output file:
Code:
SNP   BP   CHR  P     P
snp1  1     3     0.01 0.06

I tried this code
Code:
ls  -t *.txt > FileList.tmp  # make a file list of all files

Code:
cat FileList.tmp | while read line
do grep snp1| awk '{ print $1 $2 }' >temp.txt 
done

is this correct?
thank you

Last edited by radoulov; 03-20-2013 at 07:05 AM.. Reason: Code tags.
# 2  
Old 03-20-2013
What output did you get from the above?

Are you getting any error?
# 3  
Old 03-20-2013
sorry i got the a file with my searched word but only column 1 and 2 aatched however i want to display the content of all columns like

Last edited by biopsy; 03-20-2013 at 07:18 AM..
# 4  
Old 03-20-2013
Are you trying to do this only for snp1 or for each snpX ?
This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 03-20-2013
thanks
i want to grep only snp1 from all files and print its columns to new file

---------- Post updated at 07:21 AM ---------- Previous update was at 05:29 AM ----------

any suggestion?
# 6  
Old 03-20-2013
Code:
awk ' BEGIN {
        printf "SNP\tBP\tCHR\tP\n"
} $1 == "snp1" {
        n = $1 "\t" $2 "\t" $3
        if ( n in R1 )
                RN[n] = RN[n] "\t" $NF
        else
        {
                R1[n] = n
                RN[n] = "\t" $NF
        }
} END {
        for ( k in R1 )
                print R1[k], RN[k]
} ' file*.txt

This User Gave Thanks to Yoda For This Post:
# 7  
Old 03-20-2013
a lazy way :
Code:
grep -i "snp1" *.txt | awk 'NR>1{print $NF;next}1' | xargs >new_file


Last edited by ctsgnb; 03-20-2013 at 12:12 PM..
This User Gave Thanks to ctsgnb For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Spacing off when files combined using awk or cat

I have 133 .txt files in a directory that I am combining into 1 file. The problem is when I use awk or cat to combine the files I get out put like this: output 85 138662360 KCNT1 86 138662962 KCNT1 82 138657053 KCNT1 83 138657635 KCNT1 95 138646881 KCNT1... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. UNIX for Dummies Questions & Answers

Grep or cat The Whole Directory PROBLEMS :(

Hi Guys This is my first post so I am not sure how things go here. I'm sorry if I'm breaking the rule or something. Feel free to correct me about that :) So as I was saying... I'd been trying to grep this folder containing 900,000 txt files but seems no luck. I get either "No such file... (6 Replies)
Discussion started by: Nexeu
6 Replies

3. Shell Programming and Scripting

Incredibly inefficient cat | grep script

Hi there, I have 2 files that I am trying to work on. File 1 contains a reference list of unique subscriber numbers ( 7 million entries in total) File 2 contains a list of the subscriber numbers and their tariff (15 million entries in total). This file is in the production system and... (12 Replies)
Discussion started by: Cludgie
12 Replies

4. Shell Programming and Scripting

Replace cat and grep with <

Hello someone told me to use OS=`awk '{print int($3)}' < /etc/redhat-release` instead of OS=cat /etc/redhat-release | `awk '{print int($3)}'` any idea for the reason ? (5 Replies)
Discussion started by: nimafire
5 Replies

5. Shell Programming and Scripting

grep or cat using sed

Is there a way using grep or cat a file to create a new file based on whether the first 9 positions of each record is less than 399999999? This is a fixed file format. (3 Replies)
Discussion started by: ski
3 Replies

6. Shell Programming and Scripting

cat -n and grep

I am not sure if using cat -n is the most efficient way to split a file into multiple files, one file per line in the source file. I thought using cat -n would make it easy to process the file because it produces an output that numbers each line that I could then grep for with the regex "^ *$i".... (3 Replies)
Discussion started by: kapu
3 Replies

7. Shell Programming and Scripting

cat /etc/passwd and grep -v on /etc/shells

Hi All, I'd like to do this cat /etc/passwd and grep -v on the /etc/shells list I'd like to find all shell that doesn't exist on the /etc/passwd. Is there an easy way without doing a egrep -v "/bin/sh|/bin/bash................"? How do I use a file /etc/shells as my list for... (4 Replies)
Discussion started by: itik
4 Replies

8. UNIX for Dummies Questions & Answers

cat, grep and tee to a local file

Hi, This is what I am trying to do. 1) connect to 3 remote servers from my local machine serverA serverB serverC 2) read error file from each server cat /var/lib/mysql/mydb.err 3) grep for lines displaying "yesterday" date grep "`date +%y%m%d' '-d\"1 day ago\"`" 4) Append those lines to a... (7 Replies)
Discussion started by: shantanuo
7 Replies

9. UNIX for Advanced & Expert Users

cat and grep not working

I am trying to cat a file and then grep that file for a number. I can do it fine on other files but this particular file will not do anything. I tried running it on an older file from the same device but it is just not working. The file is nothing more than a flat file on a unix box. Here is just a... (3 Replies)
Discussion started by: jphess
3 Replies

10. Shell Programming and Scripting

grep command with combined variables in Ksh

Hi, I'd like to grep two variables that are seperated with one space in ksh. var1="Feb" var2="09" I tried to grep "$var1 ""$var2" /usr/file It turn into grep Feb 09 /usr/file when it runs. I got error. Can some one help me with this one? Thanks a lot for your help! (4 Replies)
Discussion started by: cin2000
4 Replies
Login or Register to Ask a Question