Using shell to replace characters in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using shell to replace characters in a text file
# 8  
Old 09-16-2011
You haven't told us how you're converting this XML to text, so it's STILL difficult to really get a grip on your input data. But I see you've shown what the URL for the XML data actually is, which lets me do this:

Code:
$ cat geektool.sh
#!/bin/bash

wget -q http://www.tdbimg.com/ext/rss/cheatsheet/rss_cheatsheet.xml -O - |
# Convert xml into text.  Set TZ to our time offset.
        awk -v TZ="$(date +%:z)" -v RS="<entry>" -v FS="\n" 'BEGIN {
        # Convert [+-]HH:MM into an integer value in seconds
        split(TZ, A, ":");
        if(A[1] < 0)    OFF=((A[1]*60) - A[2])*60;
        else            OFF=((A[1]*60) + A[2])*60;
}

NR>1{   # Fix those pesky embedded tags
        gsub("&lt;", "<", $0);  gsub("&gt;", ">", $0);
        # Loop through each line, looking for the ones we want
        for(N=1; N<=NF; N++)
        {
                if($N ~ /<(title|summary)/)
                {       # Strip out tags
                        gsub(/ *<[^>]+>/, "", $N);
                        print $N
                }
                else if($N ~/<updated/)
                {       # Strip out tags
                        gsub(/<[^>]+>/, "", $N);
                        # Convert from YYYY-MM-DD to YYYY MM DD etc.
                        gsub(/[-T:Z]/, " ", $N);
                        # Convert from UTC to local time
                        print strftime("%F %T", mktime($N)+OFF);
                }
        }

        printf("\n");
}'

$ ./geektool.sh
Quake Hits Off Japan Coast
2011-09-16 14:30:00
No tsunami warning issued.

Microsoft to Kill Windows XP
2011-09-16 14:25:00
Will not extend support beyond April 2014.

Pitt Felt 'Pathetic' While Married to Aniston
2011-09-16 13:17:00
Dishes on relationship to 'Parade' magazine.

29 Protesters Killed in Syria
2011-09-16 11:47:00
Most violent day in weeks.

Clinton: Women Can Rescue the Economy
2011-09-16 11:04:00
Delivers declaration at APEC summit in San Francisco.

Fall TV Is a Step Back for Women
2011-09-16 10:56:00
Despite more female producers, directors, actors.

Abbas to Seek Palestinian State
2011-09-16 10:21:00
At U.N. Security Council.

Police Pressure Guardian to Reveal Sources
2011-09-16 10:17:00
Threaten paper with espionage law.

Two Men Use Dead Friends ATM
2011-09-16 08:40:00
Drive around with corpse, use his cash at strip club.

Congress's Ratings Hit Record Low
2011-09-16 08:30:00
Just 12 percent of Americans approve.

Rogue Trader Charged With Fraud
2011-09-16 07:42:00
After losing $2B in unauthorized trade.

New Dolphin Species Discovered
2011-09-16 07:09:00
In Australian bay.

Rogue Trader Told Bank of Error
2011-09-16 05:16:00
UBS hadn't detected the $2B loss.

Casey Anthony Must Pay $97K
2011-09-16 05:14:00
To reimburse law enforcement for search.

FAA Shutdown Averted
2011-09-16 05:10:00
Thousands would have been furloughed Saturday.
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search special characters in a file and replace with meaningful text messages like Hello

Search special characters in a file and replace with meaningful text messages like Hello (2 Replies)
Discussion started by: raka_rjit
2 Replies

2. Shell Programming and Scripting

How to replace characters in a file?

Hi Gurus, I need replace some charactors in a file. in following example. I need replace from 4th charactor to 6th charactor with x in each line. abcdefghijklmn 123456789011 excepted result: abcxxxghijklmn 123xxx789011 Thanks in advance. (6 Replies)
Discussion started by: ken6503
6 Replies

3. Shell Programming and Scripting

Replace first 3 characters in a unix file in all lines

Replace first 3 characters in a unix file (say replace "A&B" with "C&D") in all lines of the file. Need a sed or awk script to do this. Kindly help! -Kumar (4 Replies)
Discussion started by: vasan2815
4 Replies

4. Shell Programming and Scripting

how to replace a text in a file from second file in shell

Hi, I have file 1 say abc abcd abc abcd <some tag> xyx abcd xyz abc abcd <some tag> xyx xyz abc And i have another file say file 2 - replaced tag1 replaced tag2 Now i want to put value of file2 in file1 whereever it finds <some tag>. for e.g. first <some tag> should be replaced... (4 Replies)
Discussion started by: abhitanshu
4 Replies

5. Shell Programming and Scripting

share a shell script which can replace weird characters in directory or file name

I just finish the shell script . This shell can replace weird characters (such as #$%^@!'"...) in file or directory name by "_" I spent long time on replacing apostrophe in file/directory name added: 2012-03-14 the 124th line (/usr/bin/perl -i -e "s#\'#\\'#g" /tmp/rpdir_level$i.tmp) is... (5 Replies)
Discussion started by: begonia
5 Replies

6. Shell Programming and Scripting

Replace 10 characters in file

Hi, I need to replace 10 characters string (21-30) in a file with another string. I tried using cut command, i am able get these 10 charaters, but do not know how to replace them inside the file. for example file content(these are alphanumeric characters):... (3 Replies)
Discussion started by: Johny001
3 Replies

7. Shell Programming and Scripting

Replace characters then read the file without changing it

Hi All At the moment the following code works but ideally i do not want to have to change the original $1 tr "\r" "\n" < "$1" > "$1.fix" printf "\n" >> "$1.fix" mv "$1.fix" "$1" FILE=$1 coffee_out="splitmovie" coffee_fill="-splitAt" coffee_end="-self-contained -o output.mov $2"... (1 Reply)
Discussion started by: babajuma
1 Replies

8. Shell Programming and Scripting

replace characters in a file

Hi, I have a file in which i want to replace the charaters from position 3-5 with a particular string for the first line. For ex The file contains abcdefghij jkdsflsfkdk 908090900 i want to replace the characters 3-5 for the first line as 678 so, the file should look like ... (7 Replies)
Discussion started by: dnat
7 Replies

9. Shell Programming and Scripting

How to replace characters 7 through 14 of every line in a file

Hi all, I have a file with multiple lines. I want to replace characters 7 through 14 of every line with 0000000 Input: 12345678901234567890 23456789012345678901 Output 12345600000004567890 23456700000005678901 Please help. JaK (9 Replies)
Discussion started by: jakSun8
9 Replies

10. UNIX for Dummies Questions & Answers

Replace Special characters in a file

Hi, I have a data like this in a file, 402003279034002000100147626030003300010000000000002000029000000 r^M^\MW^H I need to replace those special char to some other char like # or $ Is there any ways to do it... I tried commands tr,sed and many but it was not able to replace because... (1 Reply)
Discussion started by: solai
1 Replies
Login or Register to Ask a Question