What is the difference between cat < hello.txt and cat hello (the content of hello.txt is just hello) - command-line

I created a file echo hello > hello.txt and then executed cat < hello.txt which gives me "hello", I thought using < hello.txt would mean that the variable after cat would be the content of the file which is hello. However, when I execute cat hello it gives me an error because "cat" only deals with files. Can anyone help me understand why this is the case?

Related

How to delete previous lines to the one found in grep in shell script

I want to write a shell script code with a file having these values in file
test.txt --> file
sample
#comment
jacuzzi
#comment
testing
it was the the age of wisdom4,
So, first I want to grep the line which starts with "te" , and then delete previous 4 lines to it, but only if those lines starts with "#"
like, the output should be
sample
jacuzzi
testing
it was the the age of wisdom4,
I wrote this command using awk
awk 'NR==FNR{if (/^te.*/) for (i=-4;i<=0;i++) if (what to write here?) del[NR+i]; next} !(FNR in del)' test.txt
Can someone help me with this if condition? so that it can read the previous 4 lines and delete them only if that contains #
Please check if this can help you.
techeck=`cat 2.txt|grep ^'te'|wc -l` && if [ $techeck -ge 1 ] ; then cat 2.txt|grep -v ^'#'; fi

Replace line with file content using sed

I'm trying to replace a single line in a file with the content of another file, and print the result.
#!/bin/sh
set -e
echo "a
b
c" > template.txt
echo "Hello, World!" > foo.txt
sed -e '/b/ {
d
r /dev/stdin
}' "template.txt" < "foo.txt"
Unfortunately this results in
a
c
and when I remove the d line in the sed script, I can get
a
b
Hello, World
c
How can I get rid of the b and preserve Hello, World?
Thanks, ToxicFrog, for the answer. Apparently, d means "clear pattern space and proceed immediately to next line", so I need these commands in the opposite order. r appends the contents of the file to the output immediately rather than reading it into the pattern space.
sed -e '/b/ {
r /dev/stdin
d
}' "template.txt" < "foo.txt"
This might work for you (GNU sed):
sed '/b/cHello World!' file
Change (c) any line containing b to Hello World!.

Can cmd echo take input from a file that is redirected to stdin?

As we all know, if we type echo abc it will print abc to the terminal.
But what if we put abc to a file try and type echo < try?
I tried but it just printed an empty line.
$ echo < try
$

entering a text from a text file before the first line of another file using sed

I have a text file named: "header" and I wish to insert it as the header of "anotherfile" which already contains some text.
The final output should be:
"header" text content
right above
"otherfile" text content
On ubuntu linux system I tried the following, which got me close but not quite:
cat otherfile | sed '1 r header' - > myoutputfile
the problem with this solution is that it enters all the content of "header" one line AFTER the first line of "otherfile", instead of BEFORE the first line of "otherfile".
Is there any neat way to make the content of my "header" file appear as the header of "otherfile" ?
i should mention that the following attempt
cat otherfile | sed '0 r header' - > myoutputfile
fails with the following error message:
sed: -e expression #1, char 3: invalid usage of line address 0
The answer to the question stated in your question is simply:
cat header otherfile > myoutputfile
But to answer the question in your comments: Assuming that cat otherfile is some pipeline, not actually a file, all you need is:
cat otherfile | cat header - > myoutputfile
e.g.:
$ cat file1
a
b
c
$ cat file2
foo
bar
$ cat file1 | cat file2 -
foo
bar
a
b
c
This might work for you (GNU sed):
sed -e '1h;1r header' -e '1d;2H;2g' file
or:
sed '1e cat header' file
ok, this is the solution that works for me:
cat otherfile | sed -e '2{x;G};1{h;rheader' -e 'd}' - > myoutputfile

Remove all numbers and dashes from text file?

I have an extremely large text file that I need to remove all numbers and dashes from.
Is there any way to do this all at once via command line or perl?
tr is your friend, together with the -d option to delete.
tr -d '[0-9-]' < file
To update your file, you can do tr -d '[0-9-]' < file > tmp_file && mv tmp_file file
Or also sed:
sed 's/[0-9-]//g' file
To update your file, you can use sed -i.bak 's...' file. This will perform in-place editing: file will be updated with the new content and a file.bak backup file will be created.
Test
$ cat a
hello this is -23 and 45 bla-bla
hello bye 23.
$ tr -d '[0-9-]' < a
hello this is and blabla
hello bye .