Output all data of files into one file

I have three files (can be more than 3):
cat Katty
=> d4r4ff5rf5
123.44.32.4
=>cat Manu
ffrs44frfrf
123.33.44.3
=>cat Chris
derfe5rgrg
134.4.55.4

So basically, these files are in /Users/Unknown/files/*.
Files can be more than 3. I used 3 just for example.
I want to print out in one file:

host Katty {
code: d4r4ff5rf5;
address: 123.44.32.4
}

host Manu {
code: ffrs44frfrf;
address: 123.33.44.3;
}

host Chris {
code: derfe5rgrg;
address: 134.4.55.4;
}

path="/Users/Unknown/files/*"
for i in /Users/Unknown/files/*
do
while read line
do
// I dont know how to get the name of file and data of that file (the same in all files)
done
done

Just echo $i or $line and look at the output.
You then will have other questions :wink:

I did though...But it does not help me lol

  1. To get a filename from a given path, you may use the basename utility

  2. To read contents of a file, you can use a while loop:

while read line
do
    echo "$line"
done < /path/to/file

Basically it does not give me contents of the files....I tried it already lol
path=/Users/files/
name (basename, "$path")
echo $name

Gives me error

path=/Users/Unknown/files/ 
for i in $path/*
do
    echo "Working with file: $i"
    while read line do
        echo "$line"
    done<$i
done

Any clearer?

However, you have to insert this line your own:
grep { -A4 FILE

hth

So sorry... I just copied and run it...but it gives me nothing....Anyway, thanks i will try to fix by myself

name=$(basename "$path")

Awk solution

you may try

your files

$ cat manu.txt 
ffrs44frfrf
123.33.44.3
$ cat katty.txt 
d4r4ff5rf5
123.44.32.4
$ awk '{i++;split(FILENAME,A,".");printf (i==1)?"Host"FS A[1] "{"RS "CODE :"FS$0";"RS:"Address :"FS$0";"RS"}"RS RS}{i=(i!=1)?0:1}' *.txt

resulting

Host katty{
CODE :  d4r4ff5rf5;
Address : 123.44.32.4;
}

Host manu{
CODE : ffrs44frfrf;
Address : 123.33.44.3;
}

replace

 *.txt

with your

filepath
1 Like

LOl awk?!)) basically, i will try to change on if -while statement))) Anyway thank u so much :slight_smile:

Please use code tags as well for data!

Try

awk     'FNR==1         {printf "host %s {\ncode: %s\n", FILENAME, $1}
         FNR==2         {printf "address: %s\n}\n\n", $1}
        ' Katty Manu Chris
host Katty {
code: d4r4ff5rf5
address: 123.44.32.4
}

host Manu {
code: ffrs44frfrf
address: 123.33.44.3
}

host Chris {
code: derfe5rgrg
address: 134.4.55.4
}

hey i want code to get filenames with extra column in list file with file name .extention ????