cat and output filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cat and output filename
# 8  
Old 10-22-2009
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)
Code:
file1 {
one
two
...
}

requires a different solution from
Code:
[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.
Code:
[one, two, three, four, five]

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


Quote:
Donīt exist a smal comand with cat?

e.g. grep "one" file1 /dev/null
There is certainly no small cat to do this, and grep isn't a little command like cat!

Last edited by Scott; 10-22-2009 at 05:32 PM.. Reason: fixed cut-paste error of code
# 9  
Old 10-22-2009
Code:
grep '' file /dev/null

# 10  
Old 10-22-2009
RE: cat and output filename

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.
# 11  
Old 10-23-2009
Quote:
Originally Posted by Scrutinizer
Code:
grep '' file /dev/null

Code:
grep . f1 /dev/null

Smilie
# 12  
Old 10-23-2009
Another pure shell solution
Code:
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"
Code:
#! /bin/sh
while read line; do echo "$1:$line"; done < $1

and run with:
script.sh file1
script.sh file2
etc
# 13  
Old 10-23-2009
Quote:
Originally Posted by danmero
Code:
grep . f1 /dev/null

Smilie
Smilie although it is one character shorter it also filters out empty lines, whereas '' leaves them intact.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find all files containing string not following symlinks CAT (modified) output content to /filename

This should recursively walk through all dirictories and search for a specified string in all present files, if found output manicured content (eg some regex) with CAT into a specified directory (eg /tmp/) one by one, keeping the original names This is what I have so far, which seems to... (1 Reply)
Discussion started by: lowmaster
1 Replies

2. Shell Programming and Scripting

Cat files listed in text file and redirect to new directory with same filename

I have a directory that is restricted and I cannot just copy the files need, but I can cat them and redirect them to a new directory. The files all have the date listed in them. If I perform a long listing and grep for the date (150620) I can redirect that output to a text file. Now I need to... (5 Replies)
Discussion started by: trigger467
5 Replies

3. Shell Programming and Scripting

Getting output from a file similar to cat output

I have a file # cat /root/llll 11 22 33 44 When I cat this file content to a variable inside a shell script and echo that shell script, it does not show up as separate lines. I need echo output similar to cat. cat /root/shell_script.sh #!/bin/bash var=`cat /root/llll` echo $var (2 Replies)
Discussion started by: anil510
2 Replies

4. Shell Programming and Scripting

Cat writing only one record in the output file

Hi All, I have an input file containing data as below: Input.DAT XXXXXXX|YYYYYYY|ZZZZZZZZZZ|12334446456|B|YY|111111111|111111111|111111111|111111111|15|3|NNNNNN|Y|3|AAA|111111111... (11 Replies)
Discussion started by: sagar.cumar
11 Replies

5. Shell Programming and Scripting

Recursively cat files in a directory with filename printed first.

I want to recursively cat the content of files in a directory e.g. find /etc -type f -exec cat {} \; But I want it to print the file name first and then the content. For example let's say /etc/statetab and /etc/colord.conf will be printed first then I want the output to look something like; ... (6 Replies)
Discussion started by: lewk
6 Replies

6. Shell Programming and Scripting

Cat piped output

Hello, How can I efficiently cat piped output with another file? > (awk command) | cat file1 (piped output) Thanks! (11 Replies)
Discussion started by: palex
11 Replies

7. UNIX for Advanced & Expert Users

inconsistent cat output

Hi I'm executing a menu script in which I `cat a file` but it's giving different output some times. Following is the code fragment taken from my script. while true do cat procs.configured echo ---------separator-------------- sleep 3 done when I execute this code fragment, `cat` outputs... (2 Replies)
Discussion started by: axes
2 Replies

8. UNIX for Dummies Questions & Answers

cat files from subdirectories output using same filename

Hi, I need to concatenate data files with a .mp extension that are stored in directories by year. I want to keep the same filename as an output for example: for the file name p030.mp, which resides in the following subdirectories: /2000/p030.mp /2001/p030.mp /2002/p030.mp I want to:... (4 Replies)
Discussion started by: cmshreve
4 Replies

9. Shell Programming and Scripting

cat file and parse output

Hello, I'm new to shell scripting and did a search on the forum to what I want to do but couldn't find anything. I have about 9 routers that outputs to 1 syslog file daily named cisco.year.mo.date.log ex: cisco.2009.05.11.log My goal is to make a parsing script that cats today's syslog... (2 Replies)
Discussion started by: jjrambar
2 Replies

10. UNIX for Dummies Questions & Answers

unix cat gives no output

hi, i searched the forum, but found no thread relate to this; so sorry if it's duplicated. I'm using unix cat command but it gives no output. I check permission, owner and group; all of which are OK. I could do less and vi. any suggestions? thanks, (2 Replies)
Discussion started by: notvwatcher
2 Replies
Login or Register to Ask a Question