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
sea
October 7, 2013, 11:33pm
2
Just echo $i or $line and look at the output.
You then will have other questions
I did though...But it does not help me lol
To get a filename from a given path, you may use the basename
utility
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
sea
October 7, 2013, 11:50pm
6
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
sea:
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
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
Awk solution
your file
$ 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$0RS:"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
LOl awk?!)) basically, i will try to change on if -while statement))) Anyway thank u so much
RudiC
October 8, 2013, 6:27am
11
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 ????