ke.pets-trick.com
Information

Cat command in linux

Cat command in linux


We are searching data for your request:

Forums and discussions:
Manuals and reference books:
Data from registers:
Wait the end of the search in all databases.
Upon completion, a link will appear to access the found materials.


Cat command in linux

I am working on a piece of data that is received over multiple pipes from other programs.

Currently I'm working on extracting from the pipes the data that I want to work on. I decided to use the sed command because it will allow me to do basic operations, such as the following:

cat file1 | sed -e 's/foo/bar/' -e 's/foo/bar/' | sed -e 's/foo/bar/'

The sed command would simply take from the first input what's between foo and bar, and take from the second input what's between foo and bar, and take from the third input what's between foo and bar.

The question is:

Is this a reasonable command to use? I have seen a lot of bash/shell programs used to operate on the files, but I am wondering if it's possible to do the same thing using the shell command.

A:

Well it will be fine with single characters - like this:

echo foo | sed 's/foo/bar/'

But it is not very useful - unless you want to extract each file in separate process - and do not have your pipes as arguments to the other commands.

To extract each file separately you could read each file line-by-line and process each one of them:

#!/bin/bash

for i in {1..3} , do

<, /dev/stdin done <, "$1"

<, /dev/stdin done <, "$2"

<, /dev/stdin done <, "$3"

cat $i

done

As it is not good for long scripts and pipes - better - extract all files (except the last one) with cut command:

for i in {1..2}, do

<, /dev/stdin |

while read filename, do

cut -f1 $filename

done

done

A:

One problem with your approach is that you'd get the contents of foo back, regardless of whether foo contained any newlines. That is, if foo had 3 newlines in it:

foo

... and if I sent the contents of foo back through a pipeline, I'd end up with 3 lines of foo, not 3 lines of foo with 1 newline, which is what I want.

The other problem is that you wouldn't be able to tell where one line began and another ended. Suppose you had a file with this in it:

abc

def

ghi

If I ran your command, I'd end up with this:

abc

ghe

... but I actually want this:

abc

def

ghi

Now the way to get a "word-based" version of a filename is to make use of extended regular expressions. Here's an example using the awk command:

<, /dev/stdin | awk -v RS='' '{ print """$0""" }'

Which produces the following:

"abc"

"def"

"ghi"

To incorporate this into your script, replace your while loop with the following awk command:

while read filename, do

<, /dev/stdin |

awk -v RS='' '{ print """$0""" }'

done

... and, to avoid sending a file's contents back, change the code which calls the command to this:

while read filename, do

<, /dev/stdin |

awk -v RS='' '{ print """$0""" }' >, "$filename"

done

This produces the following output:

"abc"

"def"

"ghi"

This way, your output won't include any newlines. If you don't want any newlines, use the following command instead:

while read filename, do

<, /dev/stdin |

awk -v RS='' '{ print """$0""" }' |

xargs -0 -I'{}' rm -f {}

done

Which produces the following output:

"abc"

"def"

"ghi"

... or, if you're using bash:

while read filename, do

<, /dev/stdin |

awk -v RS='' '{ print """$0""" }' |

xargs -0 -I'{}' rm -f {}

done

A:

xargs -n 1 | awk '{print}' | xargs -0 rm -rf

This seems more portable. It doesn't depend on bash.

A:

You can use this line in bash to find all the files with the extension ".txt" in your current directory and remove them:

ls *.txt | xargs -n1 -r rm -fv

A:

Use the built-in find utility to search for files that match a given pattern:

find . -name '*.txt' -print | xargs -n 1 rm -f

This is portable, bash, zsh, ksh, etc. have a similar mechanism.

If your files may have spaces in their names, and it's important that the result be a single line, use this version:

find . -name '*.txt' -print0 | xargs -0 -n 1 rm -fv

The -print0 option for find is new in the POSIX standard, but supported by recent versions of find on most POSIX-compliant systems. It causes the results to be printed on separate lines. The xargs version above uses the -n 1 option to xargs so that a single copy of rm -fv is sent to each file, rather than one copy per filename.

(On Mac OS X, I believe find comes with -print0 but you have to install GNU find to get -print0.)