How to concatenate a string value at the head of a text file [duplicate] - linux

This question already has answers here:
Unix command to prepend text to a file
(21 answers)
Closed 2 years ago.
Real nit picky Linux question.
I have a text file, call it userec. I also have a string variable 'var_a'.
I want to concatenate the string value, let just say it's 'howdy' to the top of the text file.
So something like
echo $var_a | cat usrec > file_out
where it pipes the output from the echo $var_a as a file and adds it to the top of file_out and then adds the rest of the usrec file.
So if the userec file contains just the line 'This is the second line' then the contents of file_out should be:
howdy
This is the second line.
problem is that's not what the command is doing and I do not want to create a variable to store var_a in. This is running from a script and I don't want to create any extra flack to have to clean up afterwards.
I've tried other variations and I'm comming up empty.
Can anyone help me?

If you give cat any file names then it does not automatically read its standard input. In that case, you must use the special argument - in the file list to tell it to read the standard input, and where to include it in the concatenated output. Since apparently you want it to go at the beginning, that would be:
echo $var_a | cat - usrec > file_out

I would simply do :
echo $var_a > file_out
cat usrec >> file_out

Related

Bash script file as input [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
i am trying to give an file as input to me my shell script:
#!/bin/bash
file ="$1"
externalprogram "$file"
echo 'unixcommand file '
i am trying to give the path to my file but it says always
cannot open `=/home/username/documents/file' (No such file or directory)
my path is this /home/username/Documents/file
i do this in terminal : ./myscript.sh /home/username/Documents/file
can someone help me please?
When you say
file ="$1"
with a space after "file", you're running something called file with =$1 as an argument. There probably actually is a utility called file. If you want to assign $1 to a variable called file, you don't need the space:
file="$1"
there shouldn't be a space before = in the second line.
file=$1 should be good enough.
Check what shellcheck says about your code:
^-- SC1068: Don't put spaces around the = in assignments (or
quote to make it literal).
You can read more about SC1068 case on its Github
page.
#!/bin/bash
file=$1
code $file
echo "aberto o arquivo ${file} no vscode"
I made this code snippet to demonstrate, I pass a path and it opens the file in vscode

Insert text in specific line of file by using sed. Text is including variable, which should be added into file in double quotation [duplicate]

This question already has answers here:
How to escape single quotes within single quoted strings
(25 answers)
Closed 3 years ago.
I need your help.
I am creating bash script and I have problem in part where I need to add text in line 4 of test.txt file. My text is including variable. I know how to add text with variable, but in this case this variable must be in double quotation.
So, I am using this command:
sed -i "4s/$/Username = "$var1"/g" $dir/output/test.txt
but I get next results in test.txt file:
$Username = example
I tried many options but I can not achive to get variable in double quotation:
$Username = "example"
sed -i "4s/$/Username = \"$var1\"/" $dir/output/test.txt
Global switch is meaningless and can be removed since you're appending to the end of the line

Append text to a file with pattern matched name in bash [duplicate]

This question already has an answer here:
In bash, how do I expand a wildcard while it's inside double quotes?
(1 answer)
Closed 4 years ago.
I am trying to add a line to a specific file which matches a pattern in its name.
e.g. I am trying to append text STATUS PASSED to a file whose name contains 2018_09_26_04_51_30.
date="2018_09_26_04_51_30"
echo "STATUS PASSED" >> "/test_dir/*$date*.txt"
Above said commands are creating new file named *2018_09_26_04_51_30*.txt which is not serving my purpose!
Let's assume that there are several other files with *.txt extension, but none of these files contains $date in their names.
The test_dir directory contains:
test1-2018_09_26_04_50_48.txt
test2-2018_09_26_04_50_56.txt
test3-2018_09_26_04_51_03.txt
test3-2018_09_26_04_51_30_51S.txt
So, here file test3-2018_09_26_04_51_30_51S.txt is unique.
P.S. I have to execute this script in both Linux and AIX.
Any help will be appreciated!
Try like this,
date="2018_09_26_04_51_30"
#cd /test_dir
echo "STATUS PASSED" >> $(ls /test_dir/*$date*.txt)

Powershell script to parse a log file and then append to a file

I am new to Shellscripting.I am working on a poc in which a script should read a log file and then append to a existing file for the purpose of alert.It should work as per below
There will be some predefined format according to which it will decide whether to append in file or not.For example:
WWXXX9999XS message
**XXX** - is a 3 letter acronym (application code) like for **tom** for tomcat application
9999 - is a 4 numeric digit in the range 1001-1999
**E or X** - For notification X ,If open/active alerts already existing for same error code and same message,new alerts will not be raised for existing one.Once you have closed existing alerts,it will raise alarm for new error.There is any change in message for same error code from existing one, it will raise a alarm even though open/active alerts present.
X option is only for drop duplicates on code and message otherwise all alert mechanisms are same.
**S** - is the severity level, I.e 2,3
**message** - is any text that will be displayed
The script will examine the log file, and look for error like cloud server is down,then it would append 'wwclo1002X2 cloud server is down'if its a new alert.
2.If the same alert is coming again,then it should append 'wwclo1002E2 cloud server is down
There are some very handy commands you can use to do this type of File manipulation. I've updated this in response to your comment to allow functionality that will check if the error has already been appended to the new file.
My suggestion would be that there is enough functionality here to warrant saving it in a bash script.
My approach would be to use a combination of less, grep and > to read and parse the file and then append to the new file. First save the following into a bash script (e.g. a file named script.sh)
#!/bin/bash
result=$(less $1 | grep $2)
exists=$(less $3 | grep $2)
if [[ "$exists" == "$result" ]]; then
echo "error, already present in file"
exit 1
else
echo $result >> $3
exit 0
fi
Then use this file in the command passing in the log file as the first argument, the string to search for as the second argument and the target results file as the third argument like this:
./script.sh <logFileName> "errorToSearchFor" <resultsTargetFileName>
Don't forget to run the file you will need to change the permissions - you can do this using:
chmod u+x script.sh
Just to clarify as you have mentioned you are new to scripting - the less command will output the entire file, the | command (an unnamed pipe) will pass this output to the grep command which will then search the file for the expression in quotes and return all lines from the file containing that expression. The output of the grep command is then appended to the new file with >>.
You may need to tailor the expression in quotes after grep to get exactly the output you want from the log file.
The filenames are just placeholders, be sure to update these with the correct file names. Hope this helps!
Note updated > to >> (single angle bracket overwrites, double angle bracket appends

Read content from text file formed in Windows in Linux bash [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 9 years ago.
I am trying to download files from a database using wget and url. E.g.
wget "http://www.rcsb.org/pdb/files/1BXS.pdb"
So format of the url is as such: http://www.rcsb.org/pdb/files/($idnumber).pdb"
But I have many files to download; so I wrote a bash script that reads id_numbers from a text file, forms url string and downloads by wget.
!/bin/bash
while read line
do
url="http://www.rcsb.org/pdb/files/$line.pdb"
echo -e $url
wget $url
done < id_numbers.txt
However, url string is formed as
.pdb://www.rcsb.org/pdb/files/4H80
So, .pdb is repleced with http. I cannot figure out why. Does anyone have an idea?
How can I format it so url is
"http://www.rcsb.org/pdb/files/($idnumber).pdb"
?
Thanks a lot.
Note. This question was marked as duplicate of 'How to concatenate strings in bash?' but I was actually asking for something else. I read that question before asking this one and it turns out my problem was with preparing the txt file in Windows not really string concetanation. I edited question title. I hope it is more clear now.
It sounds like your id_numbers.txt file has DOS/Windows-style line endings (carriage return followed by linefeed characters) instead of plain unix line endings (just linefeed). The result is that read thinks the line ends with a carriage return, $line actually has a carriage return at the end, and that gets embedded in the url, causing various confusion.
There are several ways to solve this. You could have bash trim the carriage return from the variable when you use it:
url="http://www.rcsb.org/pdb/files/${line%$'\r'}.pdb"
Or you could have read trim it by telling it that carriage return counts as whitespace (read will trim leading and trailing whitespace from what it reads):
while IFS=$'\r' read line
Or you could use a command like dos2unix (or whatever the equivalent is on your OS) to convert the id_numbers.txt file.
The -e echo option is used to output the desired content without inserting a new line, you do not need it here.
Also I suspect your file containing the ids to be malformed, on which OS did you create it?
Anyway, you can simplify your script this way:
!/bin/bash
while read line
do
wget "http://www.rcsb.org/pdb/files/$line.pdb"
done < id_numbers.txt
I was able to successfully test it with an id_numbers.txt file generated like so:
for i in $(0 9) ; do echo "$i" >> id_numbers.txt ; done
Try this:
url="http://www.rcsb.org/pdb/files/"$line
$url=$url".pdb"
For more info, check How to concatenate string variables in Bash?

Resources