I have a file with 40 lines like this:

0001.Group   admin_group
0001.Users   adam, sam, paul, david, jennifer, harry
0002.Group   Dev_group
0002.Users   mike, pauli, gary, sherry
0003.Group   Sec_group
0003.Users   david, diana, mike, paul, harry
0004.Group   Main_group
0004.Users   wilson, robert, samule

I need to extract the Group and Users using the prefix and create a new file with the same name as the group.

Should look like this:

  • Output filename: admin_group, contents:

    0001.Group   admin_group
    0001.Users   adam, sam, paul, david, jennifer, harry
  • Output filename: Dev_group, contents:

    0002.Group   Dev_group
    0002.Users   mike, pauli, gary, sherry

In the end, I need to have 20 new text files. I tried using sed and awk to create two separate files and failed. How can I get this in Bash or Python?

3 Answers 3


With awk:

$ awk -v FS="\t" '$1 ~ /Group/ { file = $2 } { print $0 > file }' input.txt

awk checks if there is Group in the first column. If this is the case we store the value of the second column in the variable file.

After checking this, the whole line is printed and redirected by > to a file with the name stored in the variable file.


We can use the sed editor and run a 2-pass over the input file. First pass generates a list of sed commands to be used in the 2nd pass to generate the required output.

$ sed -ne '
      s|.*|/&/{N;w &\n}|w sed_code
 '  input-file.txt 

Then this is the second pass. Assuming the filenames to be created do not comprise any regex special characters.

$ sed -nf sed_code input-file.txt

We may also deploy Perl for this task:

 $ perl -aMautodie -pe '
    open my $fh, ">", $F[1];
    $_ .= <>;
    select $fh;
 ' input.txt

Not as clever as the awk version but here is an alternative.

497844.txt is your filename. I had used the question number

for f in `cut -f1 -d. 497844.txt | sort | uniq`
    group=`grep $f.Group 497844.txt`
    fn=`echo $group| cut -f2 -d" "`
    grep ^$f 497844.txt > $fn


For every unique group number, we get the filename which is the value of line with Group in it.

We then extract every line with this group number and redirect the output into the file derived above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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