Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


How can i use function for the below script to shorten it?


 
Thread Tools Search this Thread
# 1  
Scissors How can i use function for the below script to shorten it?

Hi All,

i worte a shell script which will zcat the .gz file and write it in to a tmp file and then again cat the file and convert it to Dos mode. Next step is i am greping the file to search for the particular string on the 1st line and if the string does not exits it will insert the 1st line .This is done on 6 different files with different headers inserting at the 1st line.

HTML Code:
zcat 2013-12-26_file1.csv.gz > 2013-12-26_file1_tmp.csv
cat 2013-12-26_file1_tmp.csv | sed 's/$/\r/' > 2013-12-26_file1.csv
grep -q "TIMESTAMP" 2013-12-26_file1.csv
if [ $? -eq 0 ]
then
echo "NO CHANGES ARE NEEDED"
exit
else
sed -i '1iTIMESTAMP|id|Fisrtname' 2013-12-26_file1.csv
echo "ADDED HEADER"
fi
 

zcat 2013-12-26_file2.csv.gz > 2013-12-26_file2_tmp.csv
cat $2013-12-26_file2.tmp.csv | sed 's/$/\r/' > 2013-12-26_file2.csv
grep -q  "ADDRESS" 2013-12-26_file2.csv
if [ $? -eq 0 ]
then
echo "NO CHANGES ARE NEEDED"
else
sed -i '1iADDRESS|ID|NAME' 2013-1-26_file2.csv
echo "ADDED HEADER"
else
fi
I thought of doing this for all the 6 files but it will be same script repeating again and again . so Can some one point me how i can reduce the code using function with small example

Thank you!!
# 2  
Code:
#!/bin/bash
 
mysub(){
if [ $1 -eq 0 ]
 then
  echo "NO CHANGES ARE NEEDED"
  exit
 else
  sed -i '1i'"$2" $3
  echo "ADDED HEADER"
 fi
)
 
zcat 2013-12-26_file1.csv.gz > 2013-12-26_file1_tmp.csv
cat 2013-12-26_file1_tmp.csv | sed 's/$/\r/' > 2013-12-26_file1.csv
grep -q "TIMESTAMP" 2013-12-26_file1.csv
mysub $? 'TIMESTAMP|id|Fisrtname' 2013-12-26_file1.csv 
 
zcat 2013-12-26_file2.csv.gz > 2013-12-26_file2_tmp.csv
cat $2013-12-26_file2.tmp.csv | sed 's/$/\r/' > 2013-12-26_file2.csv
grep -q  "ADDRESS" 2013-12-26_file2.csv
mysub $? 'ADDRESS|ID|NAME' 2013-1-26_file2.csv

Now, you can go farther, capturing file stub name by grinding it off with ${name%%.*} and suing the stub to make new names. You can pipe together stuff so you do not have intermediate files. You can take many input files from the command line or stdin.
These 2 Users Gave Thanks to DGPickett For This Post:
 

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Ps command and awk - shorten characters
SkySmart
ps -e -o pcpu -o pid -o user -o args | sort -k 1 | tail -6r %CPU PID USER COMMAND 0.3 223220 root /usr/tivoli/tsm/client/ba/bin/dsmc sched 0.2 411332 root /usr/sbin/syslogd 0.1 90962 root /usr/sbin/syncd 60 0.0 10572 root -ksh 0.0 94270 root -ksh ...... Shell Programming and Scripting
4
Shell Programming and Scripting
Shorten header of protein sequences in fasta file
alexypaul
I have a fasta file as follows >sp|O15090|FABP4_HUMAN Fatty acid-binding protein, adipocyte OS=Homo sapiens GN=FABP4 PE=1 SV=3 MCDAFVGTWKLVSSENFDDYMKEVGVGFATRKVAGMAKPNMIISVNGDVITIKSESTFKN TEISFILGQEFDEVTADDRKVKSTITLDGGVLVHVQKWDGKSTTIKRKREDDKLVVECVM KGVTSTRVYERA >sp|L18484|AP2A2_RAT AP-2...... Shell Programming and Scripting
3
Shell Programming and Scripting
How to shorten my code?
eggisbad
salary_range_report() { echo -e ${underline}$redYellow"\nSalary Range Report\n" tput sgr0 count_0_to_999=0 count_1000_to_2999=0 count_2000_to_5999=0 count_6000_to_9999=0 count_10000_above=0 for i in `cut -d "," -f4 $PAYROLL` #Loop Salary do if && then let...... UNIX for Dummies Questions & Answers
4
UNIX for Dummies Questions & Answers
script to shorten usernames and output to file
binary-ninja
Hopefully someone here can point me in the correct direction. I'm working on a username migration and am trying to map my users ols usernames to the new ones. Right now every user has a username of firstname.lastname i.e. john.doe I'm trying to create a bash or python script that will take...... Shell Programming and Scripting
3
Shell Programming and Scripting
Howto shorten script in a busybox environment by using for loops?
ni_hao
My satellite receiver is equipped with busybox, so a small linux version. That is why I can not use certain commands like #tomorrow in date commands or #date -d "+1 day" and thus I have to use: day1=$ I want to download every day 6 files from the internet but the filenames consist of the date...... Shell Programming and Scripting
6
Shell Programming and Scripting

Featured Tech Videos