Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How can i use function for the below script to shorten it? Post 302881095 by vikatakavi on Thursday 26th of December 2013 02:01:39 PM
Old 12-26-2013
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!!
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Howto shorten script in a busybox environment by using for loops?

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... (6 Replies)
Discussion started by: ni_hao
6 Replies

2. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

3. Shell Programming and Scripting

script to shorten usernames and output to file

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... (3 Replies)
Discussion started by: binary-ninja
3 Replies

4. UNIX for Dummies Questions & Answers

How to shorten my code?

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... (4 Replies)
Discussion started by: eggisbad
4 Replies

5. Shell Programming and Scripting

Shorten header of protein sequences in fasta file

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... (3 Replies)
Discussion started by: alexypaul
3 Replies

6. Shell Programming and Scripting

Ps command and awk - shorten characters

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 ... (4 Replies)
Discussion started by: SkySmart
4 Replies

7. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

8. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

9. Shell Programming and Scripting

Shorten header of protein sequences in fasta file to only organism name

I have a fasta file as follows >sp|Q8WWQ8|STAB2_HUMAN Stabilin-2 OS=Homo sapiens OX=9606 GN=STAB2 PE=1 SV=3 MMLQHLVIFCLGLVVQNFCSPAETTGQARRCDRKSLLTIRTECRSCALNLGVKCPDGYTM ITSGSVGVRDCRYTFEVRTYSLSLPGCRHICRKDYLQPRCCPGRWGPDCIECPGGAGSPC NGRGSCAEGMEGNGTCSCQEGFGGTACETCADDNLFGPSCSSVCNCVHGVCNSGLDGDGT... (3 Replies)
Discussion started by: jerrild
3 Replies
script(1)							   User Commands							 script(1)

NAME
script - make record of a terminal session SYNOPSIS
script [-a] [filename] DESCRIPTION
The script utility makes a record of everything printed on your screen. The record is written to filename. If no file name is given, the record is saved in the file typescript. See WARNINGS. The script command forks and creates a sub-shell, according to the value of $SHELL, and records the text from this session. The script ends when the forked shell exits or when Control-d is typed. OPTIONS
The following option is supported: -a Appends the session record to filename, rather than overwriting it. NOTES
script places everything that appears on the screen in filename, including prompts. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |CSI |Enabled | +-----------------------------+-----------------------------+ SEE ALSO
attributes(5) WARNINGS
script can pose a security risk when used in directories that are writable by other users (for example, /tmp), especially when run by a privileged user, that is, root. Be sure that typescript is not a link before running script. SunOS 5.11 30 Jan 2004 script(1)
All times are GMT -4. The time now is 11:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy