2

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.

3
  • 2
    Hi! This site is specific about Ubuntu, your question is not. You should ask on Unix & Linux. Sep 17, 2013 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. Sep 18, 2013 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 Ward
    Sep 18, 2013 at 0:09

3 Answers 3

2

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
1
  • Ohh, thats great. Thank you slm. I will ckeck this one tomorrow and will let you know. Sep 18, 2013 at 17:42
1

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.

2
  • Dear steeldriver, I think its working for me. I will try for my rest files. If got any trouble, I will contact you. Sep 18, 2013 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 Sep 18, 2013 at 11:38
0

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.+' *

2
  • See the difference between Text-file-2 and my desired output. I do not want to print the data for "Carbon" and "Oxygen". Sep 17, 2013 at 16:41
  • Dear josten, thank you for your kind efforts. But this is grepping approx all data from file2. Meanwhile script of @steeldriver worked for me. Sep 18, 2013 at 5:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .