1

I have output like ; (The columns separated by tab \t)

name1   something1
name1   something2
name1   something3
name2   something4
name2   something5

For this output I need two output (if there is name3, I will need 3 output) like

name1   something1
name1   something2
name1   something3

and

name2   something4
name2   something5

I think this will be done by AWK but I couldn't create magic words.

What is the best way to do this?

I need a condition to read $1 "1.column" and print all of them(not delete duplicates) unless it will change and print other columns ($2,$3,...)

I think using loop it prints first output and so on.

2

Try this:

awk -F'\t' '{print>$1;}' file

When the above command is complete, there will be two more files in the directory:

$ cat name1
name1   something1
name1   something2
name1   something3
$ cat name2
name2   something4
name2   something5

How it works

  • -F'\t'

    This tells awk to use a tab as the field separator.

  • print>$1

    This tells awk to print each line to the a file named after the first field.

Removing illegal characters from file names

Suppose the input file looks like:

$ cat file
name/1  something1
name/1  something2
name/1  something3
name/2  something4
name/2  something5

The following code creates files based on the name field but with the / removed:

awk -F'\t' '{name=$1; gsub(/[/]/, "", name); print>name;}' file

The above was tested on GNU awk and ran successfully. If your awk does not accept , then try:

awk -F'\t' '{name=$1; gsub("/", "", name); print>name;}' file

or:

awk -F'\t' '{name=$1; gsub(/\//, "", name); print>name;}' file
  • 1
    lol thats elegant! – theoden Aug 11 '15 at 21:48
  • AWK cannt open "name1" for output? It wont create a file? – makgun Aug 11 '15 at 21:53
  • @makgun That likely means that the command is being run in a directory for which you do not have write permission. Before running the command, cd to a directory that you own. – John1024 Aug 11 '15 at 21:55
  • I am at $HOME in my bash-shell – makgun Aug 11 '15 at 21:57
  • The problem cause for meta charecters which doesnt allowed by system to be named a file like : / – makgun Aug 11 '15 at 22:00
0

I think this should work:

mkdir tmp; cd tmp
while IFS= read line; do
    echo "$line" >> $(echo "$line" | awk '{print $1}')
done
cat *

This reads input line by line and appends each line accordingly to it's first argument.

If you want to stream it to variable:

while IFS= read line; do
    key="$(echo "$line" | awk '{print $1}')"
    eval "INPUT_$key='\$INPUT_$key\$line'"
done

If you have big demands from it, use:

#!/usr/bin/python

import sys
import re

for line in sys.stdin:
    f = open(re.split("\s+", line, 1), 'a')
    f.write(line)
    f.close()

This will work. Must. It can't fail.

  • With this , it prints just $1 and it wont find last line if it changes – makgun Aug 11 '15 at 21:43
  • @makgun, it will, if you press enter. – theoden Aug 11 '15 at 21:49
  • I created bash script and I added this to file with adding < <(cat $file) after done but it didnt work – makgun Aug 11 '15 at 22:03
  • @makgun, if you are planning to use all kinds of characters on all platforms, don't use bash/awk/gawk/etc, use perl/python. – theoden Aug 11 '15 at 22:33
  • I don't know how phyton works and I need to change all previous command to get this my first output – makgun Aug 11 '15 at 22:40

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.