Linux script that reads a input file - linux

I would like to seek for your help on how to write a simple bash script that reads an input file (e.g. txt file) that has a list into it (e.g. numbers/letters), process it and use that list to delete single/multiple lines on a different txt file. I tried using sed with no luck.
Hope someone can help me...
Thanks in advance and more power!

To read a file, you can use the following code:
#!/bin/bash
file="$1"
while read line
do
#process each line
done < file

Related

How to spilt a files into chunks and store no.of chunks,file name and size to json?

I want to automate this proces with linux shell script(bash script)
eg:
afile.txt
bfile.txt
cfile.txt
dfile.txt
efile.txt
ffile.txt
gfile.ps1
only .txt files to be divided into chunks of 1000bytes
eg:
afile.txt00
afile.txt01
afile.txt02
after that need to create a json containing like
{"avaible_files":[["afile.txt",2056,"1.0",3],["bfile.txt",948,"2.0",1],["cfile.txt",1054,"1.001",2],["dfile.txt",3085,"3.0",4],["efile.txt",9685,"1.0.0",10],["efile.txt",6985,"1.0.2",7],["dfile.txt",65,"1.0.0",1],["ffile.txt",9996,"3.1.0",10],["gfile.txt",785,"2.0.0",1]]}
in the json, data format is [file name,size,version,chunks]
Here version is hard coded text written inside the .txt file
function M.version()
return "1.0"
end
please help me writing bash script that will do this job
Thanks in advance
Here is the first part:
ls *.txt | while read FILE; do split -b 1000 -d $FILE $FILE; done
Second part is less clear to me...

bash, adding line after specific line

For my actual creating intial server setup script i need to add a line thats saved in a variable after a specific line in a specific file.
I want to add the line:
zend_extension = $phpextensiondir/ioncube_loader_lin_$phpextensionnumber.so
after the following line:
;realpath_cache_ttl = 120
in the following files:
/etc/php/$phpversionnumber/cli/php.ini
/etc/php/$phpversionnumber/cli/fpm.ini
I was looking around, but can't find any what I understand. Actually I'm new to bash scripting.
Can anybody explain? It seems that sed is not the right choice for it?
awk '{print} $0=="old line"{print "new line"}' file

linux create a file with the titles of the files inside a csv

So I have a file that has a list of file names, this file is a .csv
filename1
filename2
filename3...
and so on (there are hundreds of these)
What I want to do is output this list into their own files at the same spot as where this list is and make them into a .pdf file
so basically the folder will have
Filenamelist.csv
filename1.pdf
filename2.pdf
filename3.pdf
Any insight or help into this is greatly appreciated!
Edit:
here's my snippet of code
#!/bin/bash
if [[-f "/mnt/c/users/jesse/desktop/test/list.csv"]]
then
while IFS='|' read -r pdfid
do
touch "/mnt/c/users/jesse/desktop/test/${pdfid}.pdf"
done
fi
I'm getting an error when I try to bash this
syntax error near unexpected token `fi
Can anyone help me with this error?
I'm using ubuntu on windows.
You can use the read command in an iterative fashion, as described here: https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/
would this work?
if [[ -f "/mnt/c/users/jesse/desktop/test/list.csv"]]
then
while IFS='|' read -r pdfid
do
touch "/mnt/c/users/jesse/desktop/test/pdfid.pdf"
done < "/mnt/c/users/jesse/desktop/test/list.csv"
fi

(bash) how to add some iteraion to filename

I have the script that creates some .html and .txt files every day. But now it is only one file html and txt with changed content, I need every day a new html&txt file with date oof creation in the file name like : index_22-05-2013.html , i have these variables in shell script:
DESTINATION_HTML="./daily/html/index_$(date +"%F").html"
DESTINATION_TXT="./daily/txt/index_$(date +"%F").txt"
and a line in shell script that running one python script and creates html file
python `somescript.py` -m ${FILELIST[0]} ${FILELIST[1]} > $DESTINATION_HTML
and i`m getting this file created:
index_$(date +"%F").html
what i must to do to get this file name : index_22-05-2013.html
Sorry, I am not following you, but since
echo "index_$(date +%F).html"
outputs index_2013-08-20.html instead of index_22-05-2013.html which is what you need, you probably want to use this command instead:
echo "index_$(date +%d-%m-%Y).html"
Hope it helps! :)

Bash Variables containing filepaths

I'm writing a script that needs to find a file in a directory based on the user input. That file contains a filepath, and I need to use that filepath as a variable so I can use it later in a mv command. So far :-
read x
path = `cat ~/filepaths/$x`
Later it needs to move a file from trash using the filepath read from this file
mv ~/trash/$x $path
Currently, it doesn't appear to work, and hangs when it runs. Is there something stupid I've missed here?
EDIT: Solved, was a stupid syntax mistake. Thanks for your help!
Remove the spaces around the assignment:
path=`cat ~/filepaths/$x`
or:
path=$(< ~/filepaths/$x)

Resources