cat and output filename

Hi,

how I can display a file with cat and printing the filename before each line.

e.g.

file1 {
one 
two 
three 
four 
five
}

Output:

file1:one
file2:two
file1:three
file1:four
file1:five 

THANKS

Where is this coming from?

Hi.

It's simpler with AWK, otherwise you need cat plus maybe sed or awk:

awk '{print FILENAME ":" $0}' file1

file1:one
file1:two
file1:there
file1:four
file1:five

Are "file1 {" and "}" in the file?

hi
file2 is a mistake...
no, the content of file1 is [one, two, three, four, five] for example

Don�t exist a smal comand with cat?

e.g. grep "one" file1 /dev/null

grep -H '' infile
grep -H  . file

Hi,
the option -H dont make.

Hi.

You mean that -H is not available, or that the grep statement itself doesn't work. It would help everyone if you
a) stated your problem more clearly. Don't use things like "for exmaple", unless the example is reprasentative of your problem (which it wasnt in this case)

file1 {
one
two
...
}

requires a different solution from

[one, two, ...]

b) don't just say "it doesn't work". Explain WHAT doesn't work

-H is not a standard option (and you may not find it on many systems (certainly not AIX)).

In any case that wouldn't satisfy your "updated" requirement.

[one, two, three, four, five]

(by no means perfect...!)

awk -v RS=, '{ gsub(/[][ ]/, ""); print FILENAME ":" $0 }' file1
file1:one
file1:two
file1:three
file1:four
file1:five

There is certainly no small cat to do this, and grep isn't a little command like cat!

grep '' file /dev/null

You might try a script: like
#!/bin/bash
# precat : cat a file with the filename prepended to each line
FILE=$1
if [ -z "$FILE" ] ; then
echo " no file specified, and I do not care to handle stdin today"
exit 1
fi
FOO=`basename $FILE`

if [ -f $FILE ] ; then
cat ${FILE} | while read line
do
echo "$FOO --- $line "
done
else
echo "Cannot read $FILE "
fi

It messes with the formatting a bit, but is a easy 'first cut' that only requires shell.

grep . f1 /dev/null

:rolleyes:

Another pure shell solution

while read line; do echo "file1:$line"; done < file1

If you want to do it to multiple files, put it in a script and replace "file1" with "$1"

#! /bin/sh
while read line; do echo "$1:$line"; done < $1

and run with:
script.sh file1
script.sh file2
etc

:wink: although it is one character shorter it also filters out empty lines, whereas '' leaves them intact.