1

i have one file with follow entries, is very long so show few examples:

    media-libs/gd fontconfig xpm
    media-sound/audacious -gtk gtk3
    net-analyzer/wireshark -gtk3 -ssl -qt4 qt5 lua geoip kerberos adns
    net-dialup/ppp -gtk

I need to copy every line in a new file called like the name between / and first spaces.

So that i have a file called gd, audacious, wireshark and ppp with appropriate content behind name.

    media-libs/gd fontconfig xpm
    media-sound/audacious -gtk gtk3
    net-analyzer/wireshark -gtk3 -ssl -qt4 qt5 lua geoip kerberos adns
    net-dialup/ppp -gtk

Thank you for help & wish nice weekend

Silvio

Silvio
  • 123
  • 1
  • 9

2 Answers2

2

I think you need something like this, but please try it in a separate directory where you just have a COPY of your data:

awk '{f=$1;sub(/.*\//,"",f);print > f}' yourFile

It may not be that robust, but it takes the first field of each line, strips everything up to a slash and saves that as f, then writes the whole line to file f.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thank you, works perfekt. A short line and i try since hours. – Silvio Dec 19 '15 at 14:51
  • A little simpler is to use the `-F` option to split each line into appropriate fields, rather than use the `sub` function: `awk -F'/| ' '{print > $2}'`. – chepner Dec 19 '15 at 17:55
  • 1
    @chepner Thank you, yes, that is a possibility but I wasn't sure if there may be multiple slashes which would move the field numbers along. – Mark Setchell Dec 19 '15 at 18:55
0

When you want to loop with a while loop, you can look at:

while read -r line; do
        echo "line=${line}"
        noSlash=${line##*/}
        echo "noSlash=${noSlash}"
        filename=${noSlash%% *}
        echo "filename=${filename}"
        content=${noSlash#* }
        echo "Content=${content}"
done < input

The input file at the last line of the code is parsed 1 line each time. The variables are parsed using # and %. The result is echoed in the above code for testing. WHen you are happy with the solution, you can use

while read -r line; do
        noSlash=${line##*/}
        filename=${noSlash%% *}
        content=${noSlash#* }
        printf "%s\n" "${content}" > "${filename}"
done < input
Walter A
  • 19,067
  • 2
  • 23
  • 43