Visit Our UNIX and Linux User Community


Perl or Awk script to copy a part of text file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl or Awk script to copy a part of text file.
# 15  
Old 11-04-2009
I changed it but I'm getting a syntax error

Document.bash[18]: 0403-057 Syntax error at line 20 : `(' is not expected.
# 16  
Old 11-04-2009
What would be nice is if i could get a sample of the file you want to parse.
I've modified the code
Code:
LINE="lbla bla bla jcjfd<text>what i want</text>flh%(j blablabla<text>second occurence</text>juhgfiuhf<text>third occ</text>jkhgq"
I=1
LINE=${LINE#*<text>}	# Removes all from the begining up to the first "<text>"
LINE=${LINE%</text>*}	# Removes all from the end down to the last "</text>"
while echo $LINE | grep -q "<text>"	# more than one field
do	TEXT[$I]=${LINE%%</text>*}
	LINE=${LINE#*<text>}
	let I=I+1
done
TEXT[$I]=${LINE%</text>*}	# for the last one
{	# To see what we've done
	N=$I
	I=1
	while [ $I -le $N ]
	do
		echo "TEXT[$I] = ${TEXT[$I]}"
		let I=I+1
	done
}

# 17  
Old 11-04-2009
Thanks Frans this code is working, just changed <text> to "<text>" .
Can you let me know how to modify this code to accomodate input and output file.

Thanks.

Last edited by asandy1234; 11-04-2009 at 02:16 PM..
# 18  
Old 11-04-2009
Revisited

Revisited and uncommented script for in from file and out to file
Code:
I=1
while read LINE
do
	LINE=${LINE#*<text>}
	LINE=${LINE%</text>*}
	while echo $LINE | grep -q "<text>"
	do
		echo "$I ${LINE%%</text>*}"
		LINE=${LINE#*<text>}
		let I=I+1
	done
	echo "$I ${LINE%</text>*}"
        let I=I+1
done < inputfile > ouputfile

The output gives numbered lines with the text. If you don't want the numbers, remove the $I in the echo
If this doesn't work, send me a big sample of xml to process
# 19  
Old 11-04-2009
Wow great Frans, the code works. Thank you very much for ur help.

---------- Post updated at 02:14 PM ---------- Previous update was at 01:33 PM ----------

Hi Frans, one more issue.
The code works fine if the file is one line but if the xml is multi line the code doesn't work.
Please find below the xml
=====================================================

Last edited by asandy1234; 11-04-2009 at 08:09 PM..
# 20  
Old 11-04-2009
A version with numbering
Code:
LINE=`cat inputfile`
I=1
{
    LINE=${LINE#*<text>}    # Removes all from the begining up to the first "<text>"
    LINE=${LINE%</text>*}    # Removes all from the end down to the last "</text>"
    while echo $LINE | grep -q "<text>"    # more than one field
    do
    echo "$I ${LINE%%</text>*}"
        LINE=${LINE#*<text>}
        let I=I+1
    done
    echo "$I ${LINE%</text>*}"    # for the last one
} > ouputfile

Or without (simplier)
Code:
LINE=`cat inputfile`
{
    LINE=${LINE#*<text>}    # Removes all from the begining up to the first "<text>"
    LINE=${LINE%</text>*}    # Removes all from the end down to the last "</text>"
    while echo $LINE | grep -q "<text>"    # more than one field
    do
    echo "${LINE%%</text>*}"
        LINE=${LINE#*<text>}
    done
    echo "${LINE%</text>*}"    # for the last one
} > ouputfile

Take in the first line around input file that are backticks [AltGr-7] and not single quotes !
# 21  
Old 11-05-2009
Great thanks Frans

---------- Post updated 11-05-09 at 04:18 PM ---------- Previous update was 11-04-09 at 07:09 PM ----------

Hi Frans,
I really appreciate the time and effort u r spending on this, I've a request, can this code be changed to read from a DB2 table and write to DB2 table?

Thanks,

Previous Thread | Next Thread
Test Your Knowledge in Computers #43
Difficulty: Easy
A serial interface transmits two bits of information at the same time, both a one and a zero.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in UNIX shell to copy part of file name to new file name

Hi, I want to do the following in a Unix shell script and wonder if someone could assist me? I want to take files in a specific directory that start with the name pxpur012 and copy them to the same directory with the file name not containg pxpur012. For example, I have files like... (4 Replies)
Discussion started by: lnemitz
4 Replies

2. Shell Programming and Scripting

Not able to copy the file in perl cgi script

Hello experts, I am facing an very typical problem and hope the issue can be solved. I have a page download.cgi in /cgi-bin folder. use CGI; use CGI::Carp qw ( fatalsToBrowser ); use File::Copy copy("C:\\Program Files\\Apache Software... (8 Replies)
Discussion started by: scriptscript
8 Replies

3. Shell Programming and Scripting

Copy a file from local host to a list of remote hosts --- perl script

Hi friends, i need to prepare a script ( in perl) i have a file called "demo.exe" in my local unix host. i have a list of remote hosts in a file "hosts.txt" now i need to push "demo.exe" file to all the hosts in "hosts.txt" file. for this i need to prepare a script(in perl, but shell... (5 Replies)
Discussion started by: siva kumar
5 Replies

4. Shell Programming and Scripting

Copy part of file between two strings to another

I am a newbie to shell scripting I have a large log file , i need to work on the part of the log file for a particular date. Is there a way to find the first occurance of the date string and last occurance of the next day date date string and move this section to a new file. to explain it... (3 Replies)
Discussion started by: swayam123
3 Replies

5. UNIX for Dummies Questions & Answers

Copy the last part since the file has been updated

Input File1 constatntly running and growing in size. My Program Erorr ddmmyy hh:mm:ss My Program Error **Port 123 terminated **ID PIN 12345 Comamnd Successful Command Terminated Command Successful Command Terminated **My Program Erorr ddmmyy hh:mm:ss My Program Error **Port 345... (3 Replies)
Discussion started by: eurouno
3 Replies

6. UNIX for Dummies Questions & Answers

Copy a part of file

Hi, I want to copy text between expressions ">bcr1" and ">bcr2" to another file. Any simple solutions? Thanks (4 Replies)
Discussion started by: alpesh
4 Replies

7. Shell Programming and Scripting

search needed part in text file (awk?)

Hello! I have text file: From aaa@bbb Fri Jun 1 10:04:29 2010 --____OSPHWOJQGRPHNTTXKYGR____ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline My code '234565'. ... (2 Replies)
Discussion started by: candyme
2 Replies

8. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 Replies

9. Shell Programming and Scripting

awk, perl Script for processing a single line text file

I need a script to process a huge single line text file: The sample of the text is: "forward_inline_item": "Inline", "options_region_Australia": "Australia", "server_event_err_msg": "There was an error attempting to save", "Token": "Yes", "family": "Family","pwd_login_tab": "Enter Your... (1 Reply)
Discussion started by: hmsadiq
1 Replies

10. UNIX for Dummies Questions & Answers

Shell script to search for text in a file and copy file

Compete noob question.... I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory help please :eek: (9 Replies)
Discussion started by: imeadows
9 Replies

Featured Tech Videos