bash script to split and reverse filenames - bash

A bash script request(or fish script). I have a bunch of files like:
name - lastname.info
names - lastnames.info
name & name - lastname & lastname.info
but I want to change all of them to:
lastname - name.info
lastnames - names.info
lastname & lastname - name & name - lastname & lastname.info

going by your sample data, you have " - " as the delimiter, so
for file in *; do
ext=${file##*.}
no_ext=${file%.*}
name=${no_ext% - *}
lastname=${no_ext##* - }
mv "$file" "$lastname - $name.$ext"
done
For the gory details, see http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

You could use a for loop to loop through all the files in the cwd, then use the cut utility to parse the file name based on the '-' delimiters to get first and last name (I then re-cut to parse out the file name extension), then use tr to remove the leading/trailing spaces. Finally concatenate, and you've got your new file name:
for file in *
do
fname=`echo $file | cut -d '-' -f 1 | tr -d ' '`
lname=`echo $file | cut -d '-' -f 2 | cut -d '.' -f 1 | tr -d ' '`
newname=$lname' - '$fname.info
echo $newname
done

Assuming all your files are in one directory:
for f in lastname*
do
mv $f `echo $f | sed 's/lastname/name/'`
done
If your files are anywhere in the hierarchy, you will have to use the find command but the basic idea remains.

Related

How do i print multiple variables in separate lines into a file using shell scripting

i need to select string from one csv file to another properties file using shell
project.csv - this is the file which contains data like below & this may contain N number of lines/data
PN549,projects.pn549
SaturnTV_SW,projects.saturntv_sw
Need to collect each string "pn549" , "saturntv_sw" into a properties file
properties
[projects]
pn549_pt=pn549
saturntv_sw_pt=saturntv_sw
Below is the code i wrote to fetch the string and to print
cat "project.csv" | while IFS='' read -r line; do
Display_Name="$(echo "$line" | cut -d ',' -f 1 | tr -d '"')"
project_name="$(echo "$TEMP_Name" | cut -d '.' -f 2)"
echo "$project_name"
echo "$project_name"_pt="$project_name" > /opt/properties
How do i print multiple lines like i gave in example(properties)
i have got my answer, simply redirected the output

How to display 0-nth character in a file name

I have a file = 'test_acn_mark_down_201400000.csv'.
I wanted to have a value only file1='test_acn_mark_down' in unix
which means from position 0 to f4 and the delimiter will be '-'.
Please help me .
You can use cut:
file='test_acn_mark_down_201400000.csv'
echo "$file" | cut -d _ -f1-4
file1=$(echo "echi $file" | cut -d _ -f1-4)
echo "$file1"
test_acn_mark_down
You asked for the first four fields.
The best way will be using cut:
file=test_acn_mark_down_201400000.csv
file1=$(echo "${file}" | cut -d _ -f1-4)
When you know that the remaining part of the filename is without '_' characters, you can also use a special syntax removing everything on the end starting with the last underscore:
file=test_acn_mark_down_201400000.csv
file1="${file%_*}"

Extracting a substring

I have to find a substring where my string starts with country=" and ends with " like following-
country="NZ"
I have to extract only NZ part and add it to an existing string like-
string+=NZ
Please helP!!!
Use sed in regular expression mode:
string=""
INPUT='country="NZ"'
string+=$(echo $INPUT | sed -r 's/country="(.*?)"/\1/')
If your string truly only contains country="CODE", then cut works too, using " as the delimiter:
echo 'country="NZ"' | cut -d\" -f2

Looking for exact match using grep

Suppose that I have a file like this:
tst.txt
fName1 lName1-a 222
fname1 lName1-b 22
fName1 lName1 2
And I want to get the 3rd column only for "fName1 lName1", using this command:
var=`grep -i -w "fName1 lName1" tst.txt`
However this returns me every line that starts with "fName1 lName1", how can I look for the exact match?
Here you go:
#!/bin/bash
var=$(grep -Po '(?<=fName1 lName1 ).+' tst.txt)
echo $var
The trick is to use the o option of the grep command. The P option tells the interpreter to use Perl-compatible regular expression syntax when parsing the pattern.
var=$(grep "fName1 lName1 " tst.txt |cut -d ' ' -f 3)
you can try this method:
grep -i -E "^fName1 lName1\s" tst.txt | cut -f3,3- -d ' '
But you must be sure that line starts with fName1 and you have space after lName1.

modify the contents of a file without a temp file

I have the following log file which contains lines like this
1345447800561|FINE|blah#13|txReq
1345447800561|FINE|blah#13|Req
1345447800561|FINE|blah#13|rxReq
1345447800561|FINE|blah#14|txReq
1345447800561|FINE|blah#15|Req
I am trying extract the first field from each line and depending on whether it belongs to blah#13 or blah#14, blah#15 i am creating the corresponding files using the following script, which seems quite in-efficient in terms of the number of temp files creates. Any suggestions on how I can optimize it ?
cat newLog | grep -i "org.arl.unet.maca.blah#13" >> maca13
cat newLog | grep -i "org.arl.unet.maca.blah#14" >> maca14
cat newLog | grep -i "org.arl.unet.maca.blah#15" >> maca15
cat maca10 | grep -i "txReq" >> maca10TxFrameNtf_temp
exec<blah10TxFrameNtf_temp
while read line
do
echo $line | cut -d '|' -f 1 >>maca10TxFrameNtf
done
cat maca10 | grep -i "Req" >> maca10RxFrameNtf_temp
while read line
do
echo $line | cut -d '|' -f 1 >>maca10TxFrameNtf
done
rm -rf *_temp
Something like this ?
for m in org.arl.unet.maca.blah#13 org.arl.unet.maca.blah#14 org.arl.unet.maca.blah#15
do
grep -i "$m" newLog | grep "txReq" | cut -d' ' -f1 > log.$m
done
I've found it useful at times to use ex instead of grep/sed to modify text files in place without using temps ... saves the trouble of worrying about uniqueness and writability to the temp file and its directory etc. Plus it just seemed cleaner.
In ksh I would use a code block with the edit commands and just pipe that into ex ...
{
# Any edit command that would work at the colon prompt of a vi editor will work
# This one was just a text substitution that would replace all contents of the line
# at line number ${NUMBER} with the word DATABASE ... which strangely enough was
# necessary at one time lol
# The wq is the "write/quit" command as you would enter it at the vi colon prompt
# which are essentially ex commands.
print "${NUMBER}s/.*/DATABASE/"
print "wq"
} | ex filename > /dev/null 2>&1

Resources