Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have two text files. Text-file-1 contains strings (one string per line);

C 010
C 020
C 024
.
.
.

Text-file-2 contains data in following format;

C 005 Carbon
D Carbon 1
D Carbon 2
D Carbon 3
D Carbon 4
C 010 Hydrogen
D Hydrogen 1
D Hydrogen 2
C 017 Oxygen
D Oxygen 1
C 020 Nitrogen
D Nitrogen 1
D Nitrogen 2
D Nitrogen 3
C 024 Sulphur
D Sulphur 1
D Sulphur 2
.
.
.

Text-file-1 contains 30 lines but Text-file-2 contain huge data, and in the same format as I mentioned. I can grep the text in Text-file-2 found in Text-file-1 using following command;

awk 'NR==FNR { A[$2]=1; next }; A[$2]' Text-file-1 Text-file-2 > filename

Output for this script

C 010 Hydrogen
C 020 Nitrogen
C 024 Sulphur
.
.
.

My Desired output is;

C 010 Hydrogen
D Hydrogen 1
D Hydrogen 2
C 020 Nitrogen
D Nitrogen 1
D Nitrogen 2
D Nitrogen 3
C 024 Sulphur
D Sulphur 1
D Sulphur 2
.
.
.

Now, I need an extension of this command, which could print all lines (starting with "D"), including and after this line. All lines in Text-file-2 are starting with a letter (C or D). This letter is not useful for me, but I kept it. Kindly help.

share|improve this question
2  
Hi! This site is specific about Ubuntu, your question is not. You should ask on Unix & Linux. –  Andrea Corbellini Sep 17 '13 at 15:49
2  
@AndreaCorbellini That doesn't make the question off-topic. As long as Muhammad wants answers that work on Ubuntu, and isn't interested in other Linux distributions, the question is perfectly on-topic here. –  Gilles Sep 18 '13 at 0:05
    
@AndreaCorbellini I agree with on this, this question is accepted here if all he wants is this to work on Ubuntu. Having said this, this would also have worked on Stack Overflow because coding/programming. However, in either case, the question is on topic here. –  Thomas W. Sep 18 '13 at 0:09

3 Answers 3

up vote 1 down vote accepted

Here is another possible solution, using sed

while read str; do sed -n "/^$str/,/^C/ {/^$str/p;/^D/p}" Text-file-2; done < Text-file-1

Be aware that substituting shell variables into sed expressions should be used with care. It's OK in this case because Text-file-1 contains simple alphanumeric strings, but it will fail if the shell variable contains any 'special' characters that need to be escaped within the sed expression.

share|improve this answer
    
Dear steeldriver, I think its working for me. I will try for my rest files. If got any trouble, I will contact you. –  Muhammad Sufian Sep 18 '13 at 5:15
    
OK yes please let us know if you need further help - also I have edited my answer to add a warning about general usage –  steeldriver Sep 18 '13 at 11:38

Here's an awk script that does what you want:

awk '
  NR==FNR { C[$2]=1; next }
  $1 == "C" { if (C[$2] == 1) { print; D[$3]=1 } }
  $1 == "D" { if (D[$2] == 1) print }
' f1 f2

Example

Here's some sample data.

$ cat f1
C 010
C 020
C 024

$ cat f2
C 005 Carbon
D Carbon 1
D Carbon 2
D Carbon 3
D Carbon 4
C 010 Hydrogen
D Hydrogen 1
D Hydrogen 2
C 017 Oxygen
D Oxygen 1
C 020 Nitrogen
D Nitrogen 1
D Nitrogen 2
D Nitrogen 3
C 024 Sulphur
D Sulphur 1
D Sulphur 2

Results

$ awk '
>   NR==FNR { C[$2]=1; next }
>   $1 == "C" { if (C[$2] == 1) { print; D[$3]=1 } }
>   $1 == "D" { if (D[$2] == 1) print }
> ' f1 f2
C 010 Hydrogen
D Hydrogen 1
D Hydrogen 2
C 020 Nitrogen
D Nitrogen 1
D Nitrogen 2
D Nitrogen 3
C 024 Sulphur
D Sulphur 1
D Sulphur 2

You can put the awk script into it's own file like so, cmd.awk:

NR==FNR { C[$2]=1; next }
$1 == "C" { if (C[$2] == 1) { print; D[$3]=1 } }
$1 == "D" { if (D[$2] == 1) print }

Then run it like so:

$ awk -f cmd.awk f1 f2
C 010 Hydrogen
D Hydrogen 1
D Hydrogen 2
C 020 Nitrogen
D Nitrogen 1
D Nitrogen 2
D Nitrogen 3
C 024 Sulphur
D Sulphur 1
D Sulphur 2
share|improve this answer
    
Ohh, thats great. Thank you slm. I will ckeck this one tomorrow and will let you know. –  Muhammad Sufian Sep 18 '13 at 17:42

Your wants seem to be conflicting with what is currently possible.

If you want to print D from both files use the following: cat file1 file2 | grep -E '^D.+'

The problem is that if you do a cat, you'll no longer know the file names once piping so you'll have to do something like: grep -El '^D.+' *

share|improve this answer
    
See the difference between Text-file-2 and my desired output. I do not want to print the data for "Carbon" and "Oxygen". –  Muhammad Sufian Sep 17 '13 at 16:41
    
@MuhammadSufian tie in a grep -Ev that just filters those out. –  josten Sep 18 '13 at 2:05
    
Dear josten, thank you for your kind efforts. But this is grepping approx all data from file2. Meanwhile script of @steeldriver worked for me. –  Muhammad Sufian Sep 18 '13 at 5:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.