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.
1lol thats elegant! – theoden8 – 2015-08-11T21:48:49.063
AWK cannt open "name1" for output? It wont create a file? – makgun – 2015-08-11T21:53:31.963
@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 – 2015-08-11T21:55:11.510I am at $HOME in my bash-shell – makgun – 2015-08-11T21:57:46.293
The problem cause for meta charecters which doesnt allowed by system to be named a file like
: /
– makgun – 2015-08-11T22:00:37.193Is there any way to be named it with incrementing filename instead of "$1" – makgun – 2015-08-11T22:05:22.647
@makgun See updated answer for a way to remove illegal characters from file names. – John1024 – 2015-08-11T22:05:28.533
my output;
`makgun@makgun02:~$ awk -F'\t' '{name=$1; gsub(/[/]/, "", name); print>name;}' random
awk: line 1: regular expression compile failed (bad class -- [], [^] or [)
[
awk: line 1: syntax error at or near ] ` – makgun – 2015-08-11T22:09:28.607
Curious. What OS are you using? I updated the answer with two alternative ways to write that regex. Give them a try. P.S. Thanks for supplying the complete error message. – John1024 – 2015-08-11T22:20:09.647
I used this command
awk -F'\t' '{name=$1; gsub(/[\/]/, "", name); print>name;}' file
with escaping/
with using\\
after this it creates files +1 – makgun – 2015-08-11T22:29:41.170@John1024 Long time later, I am again here, I need a command to do this to an array variable. I used
for loop
to put them into array but if there is a file which is named asname
output in current dir , it overwrites . So How can I DO assign them to an array? It must be assign in awk and also it must be recallable outside of awk (in current shell). – makgun – 2015-09-04T22:02:19.783@makgun To append, rather than overwrite, replace
>
with>>
:awk -F'\t' '{print>>$1;}' file
. – John1024 – 2015-09-09T18:52:33.907Thanks @John1024. Appending it not useful for my script but I solved this issue via using
awk and for loop
echo "$var" | awk '{print $1}' | awk '!a[$0]++' --->>> this gives me all $1 as string and I assigned all of them as array. After that I created forloop to re call array and then assigned them a new array (i usednewarray[$i]=$(awk -v a=${a[$i]} '{if ($1 == a) print $0}')
this condition and for loop.)($i is in ${!a[*]}) – makgun – 2015-09-09T19:04:08.023