If I use cp inside a bash script the copied file will have weird charachters around the destination filename.
The destination name comes from the results of an operation, it's put inside a variable, and echoing the variable shows normal output.
The objective is to name a file after a string.
#!/bin/bash
newname=`cat outputfile | grep 'hostname ' | sed 's/hostname //g'
newecho=`echo $newname`
echo $newecho
cp outputfile "$newecho"
If I launch the script the echo looks ok
$ ./rename.sh
mo-swc-56001
However the file is named differently
~$ ls
'mo-swc-56001'$'\r'
As you can see the file contains extra charachters which the echo does not show.
Edit: the newline of the file is like this
# file outputfile
outputfile: ASCII text, with CRLF, CR line terminators
I tried in every possible way to get rid of the ^M charachter but this is an example of the hundreds of attempts
# cat outputfile | grep 'hostname ' | sed 's/hostname //g' | cat -v
mo-swc-56001^M
# cat outputfile | grep 'hostname ' | sed 's/hostname //g' | cat -v | sed 's/\r//g' | cat -v
mo-swc-56001^M
This newline will stay there. Any ideas?
Edit: crazy, the only way is to perform a dos2unix on the output...
Looks like your outputfile has \r characters in it, so you could add logic there to remove them and give it a try then.
#!/bin/bash
##remove control M first from outputfile by tr command.
tr -d '\r' < outputfile > temp && mv temp outputfile
newname=$(sed 's/hostname //g' outputfile)
newecho=`echo $newname`
echo $newecho
cp outputfile "$newecho"
The only way was to use dos2unix
Related
I have a variable and that variable only needs a '\' in front of it.
I would say that the sed command is the ideal tool for it?
I tried using single quotes, double quotes, multiple variables, combination of variables, ...
I don't get an error returned but the end result is not showing what I need it do be
FOLDER=$(echo `cat file.XML | grep "Value" | cut -d \" -f2`)
echo $FOLDER
sed -i "s#"$FOLDER"#"\\$FOLDER"#g" ./file.XML
echo $FOLDER
After execution, I get
$ ./script.sh
b4c17422-1365-4fbe-bccd-04e0d7dbb295
b4c17422-1365-4fbe-bccd-04e0d7dbb295
Eventually I need to have a result like
$ ./script.sh
b4c17422-1365-4fbe-bccd-04e0d7dbb295
\b4c17422-1365-4fbe-bccd-04e0d7dbb295
Fixed thanks to the input of Cyrus and Ed Morton.
FOLDER=$(echo `cat file.XML | grep "Value" | cut -d \" -f2`)
NEW_FOLDER="\\$FOLDER"
sed -i "s#$FOLDER#\\$NEW_FOLDER#g" ./file.XML
I want to append a string variable.
for e.g. WINDOW=WINDOW winName="fp_w_RetrocessionTrigger" winTitle="Retrocession Trigger" comp="FPGUI" funcId="14316" domainId="bancs" preLoad="true">
This is my string variable(WINDOW). I want this varible of string get append in another file using sed or awk cmd.
Although I have tried the cmd like sed -i ''$n'i "'$WINDOW'"' test.xml but it only prints space there. Please help me.
Your question is not very clear, however if u want to append a text in the beginning and/or at the end of each line of a file, you can try the below
$ cat 1.txt
1
2
3
4
5
Add the text in the beginning of the line :
$ sed -e 's/^/start_of_the_line/' -i 1.txt
Add the text at the end of the line :
$ sed -e 's/$/end_of_the_line/' -i 1.txt
Output
$cat 1.txt
start_of_the_line1end_of_the_line
start_of_the_line2end_of_the_line
start_of_the_line3end_of_the_line
start_of_the_line4end_of_the_line
start_of_the_line5end_of_the_line
what is the difference between the following
cat script | sed -n ';s/\W|\W/\n/g; s/\W>\W/\n/g;p' | sed -n 's/\W/\n/1;p ' | sed -n 'x;n;x;p'
cat script | sed -n -e ';s/\W|\W/\n/g; s/\W>\W/\n/g;p' -e 's/\W/\n/1;p ' -e 'x;n;x;p'
using ;l; there is apparent difference in treating \n, and end of line $, sed doesn't treat streams delimited by \n as different lines, but each line ends with it's original end of line. is there a way to insert endofline without pipelining?
sample script
prog1 arg1 | prog2 opt1
prog3 arg3 > filename
sample output
prog1
prog2
prog3
filename
script support redirection(>), and pipelining(|), and limited executables.
output is executable name, or redirection file.
My KSH-Script should replace a String in a txt file from the same directory.
sed -i 's/"$original"/"$reversed"/' inputtext.txt
is what I'm using currently, but it doesn't work. There is no error in the code or things like that. It just doesn't work.
Here is my whole code:
#!/bin/ksh
original=$1
reversed=""
counter=0
echo $original | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \
while read char
do
letters[$counter]+="$char"
((counter=counter+1))
done
length=${#original}
((length=length-1))
echo $original | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \
while read char
do
reversed+=${letters[$length]}
((length=length-1))
done
echo $reversed
sed -i 's/"$original"/"$reversed"/' inputtext.txt
exit 0
I want, that in the file "inputtext.txt" (same dir as the .sh file) every word that equals "$original" gets changed to "$reversed".
What am I doing wrong?
I think single quotes prevent variable expansion. You can try this:
sed -i "s/$original/$reversed/" inputtext.txt
I have two .txt files, one with TAB as field delimiter and another with | as field delimiter.
I want to change the delimiter from TAB to CTRL-A and save as .txt file for the first file and for the second file change the delimiter from | to CTRL-A and save as .txt file.
These two files are separate files.
How can we do it using awk or sed?
For file one, try:
cat file1 | sed -e 's/\t/\x01/g' >file1.txt
For file two, try
cat file2 | sed -e 's/\|/\x01/g' >file2.txt
This is a great use for tr:
tr '\t' '\001' <file1 >file1-new
That will perform the transformation from horizontal tabs on file1 and output the results to file1-new. You can do the same thing with pipes.
An alternative using perl:
Replacing pipes:
echo "a|b|c" | perl -pe '$c=chr(1); s/\|/$c/g' | cat -A
a^Ab^Ac$
Replacing tabs:
echo -e "a\tb\tc" | perl -pe '$c=chr(1); s/\t/$c/g' | cat -A
a^Ab^Ac$