In Linux shell scripting if we want remove dupicate line, how can i do that except sort -u command [duplicate] - linux

This question already has answers here:
How to delete duplicate lines in a file without sorting it in Unix
(9 answers)
Closed last month.
write a script name to read file name from the end user and remove duplicate line from in that file.
#! /bin/bash
read -p "Enter any file name to remove duplicate line:" $fname
sort -u $fname > tmp.txt
mv tmp.txt > $fname
here duplicate line will be remove but my content will be sorted but i don't that what should i do.
i want another method to remove duplicate line in shell scripting.

You can remove duplicate lines by the 'uniq' command.

Related

how to copy the bash file itself to the output dir [duplicate]

This question already has answers here:
How do I know the script file name in a Bash script?
(25 answers)
Closed 2 years ago.
I want to save a copy of the bash file each time I run it. It should be saved to the output dictionary.
I am doing it like this in the mytrainrtest.sh file:
mkdir -p "${EXP_DIR}/train"
cp "${WORK_DIR}"/mytrainrtest.sh "${EXP_DIR}"/.
Now I have much more bash files with name my****** as copies of the upper one, each with different names.
How can I write the line, so the bash file will recognize its name to copy itself?
Use the $0 special variable which contains the name of the currently executing script.
cp "$0" "$exp_dir"/
Script name(path) stored in a special var $0
#!/bin/bash
echo $0
$ ./test
./test

I need a script to replace old libraries with newer library in all files [duplicate]

This question already has answers here:
How to replace a string in multiple files in linux command line
(28 answers)
How can I use a file in a command and redirect output to the same file without truncating it?
(14 answers)
Looping through the content of a file in Bash
(16 answers)
How to loop over files in directory and change path and add suffix to filename
(6 answers)
Closed 3 years ago.
I have numerous files in a directory and want to replace one of the libraries used in all of these files with a newer library because the old library no longer works.
I have used ls > names.txt to list all filenames into a txt document. I then attempted to write a bash script which would loop through all files, catch the old library, and replace it with the new library.
for entry in names.txt
do
sed 's/<libOld>/<libNew>/g' $entry > $entry
done
I expect the loop to go through each file name, find the old library used, and replace it with the new one. Running this script however doesn't appear to do anything.
You're bumping into a few common issues; I've closed as a duplicate, but here are the collected fixes for your specific case:
Editing a file in-place with sed
With GNU sed:
sed -i 's/<libOld>/<libNew>/g' filename
with any sed:
sed 's/<libOld>/<libNew>/g' filename > filename.tmp && mv filename.tmp filename
Looping over a file line by line
for entry in names.txt only ever sets entry to names.txt, it doesn't read its contents. This is also BashFAQ/001.
while IFS= read -r entry; do
printf '%s\n' "$entry"
done < names.txt
Looping over all files in a directory
You don't need a separate file, and you shouldn't use ls but globs:
for fname in ./*; do
printf '%s\n' "$fname"
done
Combined for your case
Notice the double quotes around $entry.
for entry in ./*; do
sed -i 's/<libOld>/<libNew>/g' "$entry"
done
which can be simplified to no loop at all:
sed -i 's/<libOld>/<libNew>/g' ./*

"Recursive hexdump" from command line, input and output with same name [duplicate]

This question already has answers here:
Looping over pairs of values in bash [duplicate]
(6 answers)
Closed 5 years ago.
I have some files in a directory "documents" (file1, file2, ...) and I would like to save them in another directory "documents_hex" with hexdump from command line. There is a way to use hexdump for each file in "documents" and save them in "documents_hex" ("documents_hex" is inside "documents") with the same name in input and output?
Example: file1 to /documents_hex/file1, file2 to /documents_hex/file2, ...
Check this code :
for file in `ls documents`
do
hexdump -x $file > documents_hex/$file
done

sed changes are lost (while running cat command on txt file) [duplicate]

This question already has answers here:
Find and replace in file and overwrite file doesn't work, it empties the file
(12 answers)
sed edit file in place
(15 answers)
Closed 6 years ago.
I need to insert a command "new file" in a test.txt file at line number 4.
Tried sed; I can see the changed file output, but when I again do cat test.txt, the changes are gone.
sed "4i new file" /test.txt
How can I save the changes?
Use in place edit option sed -i "4i new file" test.txt
Without the -i option sed will not make any changes to the file. It will only print the result.
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
sed '4i new file' test.txt > tmp && mv tmp test.txt

add line to a file ONLY if it is not in file already [duplicate]

This question already has answers here:
Appending a line to a file only if it does not already exist
(25 answers)
Closed 1 year ago.
I want to add the following line:
nohup java -jar /mnt/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
to the end of the file /etc/rc.d/rc.local if it does not already exist.
How can I do that from linux command line? I assume grep or sed would work, but I am not familiar enough with either to get it to work. Right now I use echo, but that just keeps adding it over and over again.
Assuming you want it at the end of the file:
LINE="nohup java -jar /mnt/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &"
FILE=/etc/rc.d/rc.local
grep -q "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
one option is two steps:
grep -q "yourline" /path/file||sed -i '/..place../ a \the line' file
also possible to do with awk,
save all lines in array, during saving if the line was found, exit. otherwise, add the line in END{} block to the right place.
P.S. You didn't tell in the file, where to add that line.

Resources