Adding line in a file using info from previous line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding line in a file using info from previous line
# 1  
Old 11-01-2014
Adding line in a file using info from previous line

I have a shell script that looks something like the following:

Code:
mysql -uroot db1 < db1.sql
mysql -uroot db2 < db2.sql
mysql -uroot db3 < db3.sql
mysql -uroot db4 < db4.sql
....


different db names in more than 160 lines.
I want to run this script with nohup and have a status later.
So, basically, I need a quick fix to insert another line after each restore command above that says the database is restored.

Code:
mysql -uroot db1 < db1.sql
echo db1 is restored
mysql -uroot db2 < db2.sql
echo db2 is restored
mysql -uroot db3 < db3.sql
echo db3 is restored
mysql -uroot db4 < db4.sql
echo db4 is restored
....

How can this be done with vi and unix utilities?
Thanks in advance,
-MKH

Last edited by Don Cragun; 11-01-2014 at 04:22 PM.. Reason: Add CODE tags.
# 2  
Old 11-01-2014
Any attempts from your side?
# 3  
Old 11-02-2014
I can make all sorts of modifications/additions on the same line. My challenge is adding the new line. I created the script using
Code:
ls -l | awk '{print $9}' > restore.sh

and then using different vi/awk commands to modify the lines into single mysql restore lines.
I ran the script overnight, but was looking for ways to report the status of the restore.

Last edited by Scrutinizer; 11-02-2014 at 01:14 AM.. Reason: code tags
# 4  
Old 11-02-2014
Code:
perl -lne 'print; print "echo $1 is restored" if /(db\d+)\.sql$/' db.script > new_db.script

# 5  
Old 11-02-2014
First: note that:
Code:
ls -l | awk '{print $9}' > restore.sh

could be much more simply and correctly written as:
Code:
ls > restore.sh

A simple way to see the progress of a script is to trace it while it runs. I.e., instead of running your script with:
Code:
sh restore.sh

run it with:
Code:
sh -x restore.sh

With your current script, adding the echo statements can easily be done with:
Code:
awk '{print;printf("echo %s is restored\n", $3)}' restore.sh

And, starting from scratch, if I understand what you're trying to do, the following should come close to automatic creation of your script:
Code:
#!/bin/ksh
for dbs in *.sql
do      db="${dbs%.sql}"
        printf 'mysql -uroot %s < %s\necho %s is restored\n' "$db" "$dbs" "$db"
done > restore.sh

# 6  
Old 11-02-2014
Quote:
Originally Posted by MKH
I can make all sorts of modifications/additions on the same line.
Looks like you didn't know that you can put two commands on the same line, separated by a semicolon. With this trick you might have been able to manage it yourself.
Code:
mysql -uroot db1 < db1.sql; echo db1 is restored

Quote:
Originally Posted by MKH
but was looking for ways to report the status of the restore.
Important note: simply echo-ing that something happend - especially like you intend to do here - is not something you can really rely on, thus I suggest following solution:
Code:
mysql -uroot db1 < db1.sql && echo "db1 is restored" || echo "db1 restore failed"

Otherwise your report will say that the db was restored even if the restore failed, for whatever the reason.
# 7  
Old 11-03-2014
Thanks for the great pointers everyone!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

2. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

Adding info to end of line if two columns match from files with different separators

I have two files (csv and vcf) which look exactly like this S1.csv func,gene,start,info "exonic","AL","2309","het" "exonic","NEF","6912","hom"S1.vcf ##fileinfo #CHROM POS ID INFO chr1 4567 rs323211 1/1:84,104,99 chr4 2309 rs346742 1/1:27,213,90 chr6 5834 ... (5 Replies)
Discussion started by: Sarah_19
5 Replies

5. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

6. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

7. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

8. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

9. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

10. Shell Programming and Scripting

Append each line to next previous line in a file

Hi all, Please help me in providing sample code to append the following 4 lines in one row. Input : A1/EXT "BAPBSC10/07B/00" 523 090530 0115 RXOCF-430 HY1711 1 EXTERNAL ALARM DOOR ALARM Output should be : A1/EXT "BAPBSC10/07B/00" 523 090530 0115 ... (8 Replies)
Discussion started by: sudhakaryadav
8 Replies
Login or Register to Ask a Question