Appending the text of a file - linux

I am using Shell scripting. I tried the below option.
I want to append the part of a file to another part of a file.
I tried this command to append the content of a file.
Command:
/bin/cat ../../../test_op.txt/sql/part_code.txt >> ../../../PartitioningUtility/log/test_op.txt
To append the text of part_code.txt to test_op.txt.
No errors but the text is not appending.
Any Solution to this problem.
Any other option to append the data of a one file to another

If you want to append all content from '/bin/cat ../../../test_op.txt/sql/part_code.txt' to '../../../PartitioningUtility/log/test_op.txt' you could add 'cat' in the beginning:
cat ../../../test_op.txt/sql/part_code.txt >> ../../../PartitioningUtility/log/test_op.txt
That is assuming that file paths are correct.

Related

BASH: How to copy the name of the file and insert it into the text using script?

So I need to create a lot of text files.
Each of the text file is named AAAAA.txt, BBBBB.txt, CCCCC.txt and etc etc.
Within each text file, all the content is as follows:
1.Copy "to-be-replaced".txt into the folder EXCLUSIVE.
2.Copy the gs file to replace the existing gs file.
3.The .projectdata should also be copied to the correct path.
So, I need to write a script, that copies the name of the file (AAAAA, BBBBB, and so on) and then place it in the "to-be-replaced" within its content.
How can I do that? need some idea please.
Thank you~~
MT32
Use a HERE document which expands variables if the delimiter isn't quoted:
#!/bin/bash
for char in {A..Z} ; do
filename=$char$char$char$char$char.txt
cat <<EOF > $filename
1.Copy $filename into the folder EXCLUSIVE.
2.Copy the gs file to replace the existing gs file.
3.The .projectdata should also be copied to the correct path.
EOF
done

sed in bash to overrewrite to same file [duplicate]

This question already has answers here:
sed edit file in place
(15 answers)
Closed 7 years ago.
I want to remove the headers of a file and replace its content without headers in the same file.
Example: file_student
name age
XYS 24
RTF 56
The output should be:
XYS 24
RTF 56
The scenario is that I do not want to create any new file for this change. Can sed do this?
I tried:
sed 1d /tmp/file_student.txt |
hadoop fs -copyfromLocal /tmp/file_student.txt /tmp/file_student_no_header.txt
But that does not work. Any help is appreciated!
an extract from sed's man page
-i[SUFFIX]'
--in-place[=SUFFIX]'
This option specifies that files are to be edited in-place. GNU
`sed' does this by creating a temporary file and sending output to
this file rather than to the standard output.(1).
This option implies `-s'.
When the end of the file is reached, the temporary file is renamed
to the output file's original name. The extension, if supplied,
is used to modify the name of the old file before renaming the
temporary file, thereby making a backup copy(2)).
This rule is followed: if the extension doesn't contain a `*',
then it is appended to the end of the current filename as a
suffix; if the extension does contain one or more `*' characters,
then _each_ asterisk is replaced with the current filename. This
allows you to add a prefix to the backup file, instead of (or in
addition to) a suffix, or even to place backup copies of the
original files into another directory (provided the directory
already exists).
If no extension is supplied, the original file is overwritten
without making a backup.
so you need to change your sed command to sth like
sed -i 1d file | whatever
hope this helps.
If you don't want to use sed, try tail - e.g. you have a file called xxx:
tail -n +2 xxx > xxx.tmp && mv xxx.tmp xxx

How to concatenate a string from an included file in bash

What I'm trying to accomplish is having a central configuration file, in bash, that defines some variables that are re-used in different bash files. The example below attempts to generate a file name with the current date included in the file name as well as a variable defined in another shell script. However whenever I try to concatenate this external variable it doesn't work. I can concatenate the variable in any other situation.
Example Code:
../config/vars.sh
#!/bin/bash
mysqlUser="backupuser"
mysqlPwd="fakePwd"
mysqlSocket="/var/run/mysqld/mysqld.sock"
mysqlPort="3306"
serverName="s01.catchyservername.com"
./dbBackup.sh
#!/bin/bash
source ../config/vars.sh
tempName=$(date +"%Y%m%d.sql.gz")
fileName="mysqld_${mysqlPort}_${tempName}"
echo "mysqld_${mysqlPort}"
echo ${tempName}
echo ${fileName}
output of dbBackup.sh
mysqld_3306
20140926.sql.gz
_20140926.sql.gz
As you can see when echoing "mysqld_${mysqlPort}" I get the expected output, but when echoing ${fileName} the entire first half of the string is ignored. What am I misunderstanding?
Your vars.sh file was probably created with a DOS/windows text editor:
$ ./dbBackup.sh
mysqld_3306
20140926.sql.gz
_20140926.sql.gz
$ dos2unix vars.sh
dos2unix: converting file vars.sh to Unix format ...
$
$ ./dbBackup.sh
mysqld_3306
20140926.sql.gz
mysqld_3306_20140926.sql.gz
$
As you can see above, I use the dos2unix utility to convert the line separators to Unix style.

Filtering text files in cmd?

Is there any way that one can filter a text file in Windows' CMD as with awk in shell script?
I have a somehow large file and I only need the last column from each row. This will be done extremely easy with awk, but I have no means of using that now.
Try this our
Get-Content .\test.csv | %{ $_.Split(',')[1]; }
or for more reference
check out this site
[1]: http://windows-powershell-scripts.blogspot.in/2009/06/awk-equivalent-in-windows-powershell.html
This will return every last term after the last comma in a .csv file for example:
#echo off
type "file.csv" | repl ".*,(.*)" "$1" >"newfile.txt"
This uses a helper batch file called repl.bat (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.

Shell script handle string with sed

I have a text file, each line is one or more file paths separated with space, all the file has suffix dl, e.g.
/some/path/file.dl
/some/other/path/file2.dl /some/other/path2/file3.dl
/some/other/path3/file4.dl /some/other/path4/file5.dl ...
...
Now I need to transform the above file to another text file. Only the first file of every line should be changed to /out/P{fileName}.h:, {fileName} is the original file name without directory and suffix. e.g.
/out/Pfile.h:
/out/Pfile2.h: /some/other/path2/file3.dl
/out/Pfile4.h: /some/other/path2/file5.dl ...
...
So how can I write the linux shell script?
Try this command:
$ sed -r 's#^\S*/(\S*)\.dl#/out/P\1.h:#' input
/out/Pfile.h:
/out/Pfile2.h: /some/other/path2/file3.dl
/out/Pfile4.h: /some/other/path4/file5.dl

Resources