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
# 1  
Old 09-14-2011
Using shell to replace characters in a text file

Can I just say, this is such a frustrating and yet enormously rewarding field of study. I'm in the middle of configuring GeekTool (Uh oh, stupid n00b) and I really only have one question.

I'm using Automator to grab a RSS feed, having GeekTool continually run that application every 10 minutes, putting the results in a text file and then displaying that .txt file. However, the RSS codes their dates by their standards and I don't know how to intercept that and change them to my own time zone.

Here's a picture:

Image

Here's the GeekTool code that runs it:

Code:
head -n 300 ~/Documents/Notes/GeekTool/RSS/Cheatsheet.txt | tr '[\n]' '~' | sed 's/~~/\
/g' | sed 's/~//g' | sed 's/-0000//g'

----

Bonus question: Why is my script cutting off the text file at "NASA unveils ..."? There's plenty room in the window and the actual text file has more content than that. Is there a character limit cut-off that I'm missing in the code?

Thanks for reading!
# 2  
Old 09-14-2011
Without seeing the data which produced that, we're only guessing.
# 3  
Old 09-14-2011
Oh okay. What other information do you need? The automator script?

The code I copied and pasted above is what I put in the "Command" line of GeekTool. There isn't a separate Shell file beyond that.
# 4  
Old 09-14-2011
Give us the original input and desired output

--ahamed
# 5  
Old 09-16-2011
Okay. So basically I'm extracting from a xml feed and converting it to plain text on a locally stored file.

Image

So the .xml is saved and converted to a plain text file

Image

Now I use GeekTool and a Shell script to display that file on my desktop

Code:
head -n 300 ~/Documents/Notes/GeekTool/RSS/Cheatsheet.txt | tr '[\n]' '~' | sed 's/~~/\
/g' | sed 's/~//g' | sed 's/-0000//g'

Image

----

Now my question is how do I take these dates that were pulled from the xml file:

Image

and alter them using a Shell script to display my desired time?

My desired output would be the date converted to my time zone: Eastern Time Zone, (UTC−05). So basically the same format "yyyy-mm-dd hh:mm:ss" (remove the +0000) but just converted to my time. Can shell script do that?
# 6  
Old 09-16-2011
Something like this?
Code:
#!/bin/bash
while read line
do
  from_date=$( echo $line | sed -n 's/.* - \([0-9]*-.*\) +0000/\1/gp' )
  if [ ! -z "$from_date" ]
  then
    to_date=$( TZ=UTC date '+%F %k:%M:%S' -d "$from_date IST" )
    line=$( echo $line | sed "s/$from_date +0000/$to_date/g" )
  fi
  echo $line
done < inputFile


root@bt:/tmp# cat inputFile 
NEW DOLPHIN SPECIES DISCOVERED - 2011-09-16 13:09:00 +0000
IN AUSTRALIAN BAY
root@bt:/tmp# 
root@bt:/tmp# ./run
NEW DOLPHIN SPECIES DISCOVERED - 2011-09-16 7:39:00
IN AUSTRALIAN BAY
root@bt:/tmp#

And please try to paste the code in test format!

--ahamed
# 7  
Old 09-16-2011
Ahamed, I hope you're patient with me. I'm still learning so much. So I pasted in your code with my previous code to make this:

Code:
head -n 300 ~/Documents/Notes/GeekTool/RSS/Cheatsheet.txt | tr '[\n]' '~' | sed 's/~~/\
/g' sed 's/~//g' | sed 's/-0000//g'

#!/bin/bash
while read line
do
  from_date=$( echo $line | sed -n 's/.* - \([0-9]*-.*\) +0000/\1/gp' )
  if [ ! -z "$from_date" ]
  then
    to_date=$( TZ=UTC date '+%F %k:%M:%S' -d "$from_date IST" )
    line=$( echo $line | sed "s/$from_date +0000/$to_date/g" )
  fi
  echo $line
done < inputFile

root@bt:/tmp# cat inputFile 
NEW DOLPHIN SPECIES DISCOVERED - 2011-09-16 13:09:00 +0000
IN AUSTRALIAN BAY
root@bt:/tmp# 
root@bt:/tmp# ./run
NEW DOLPHIN SPECIES DISCOVERED - 2011-09-16 7:39:00
IN AUSTRALIAN BAY
root@bt:/tmp#

Now nothing displays. Maybe that's a GeekTool thing. In fact, maybe I should put your code in a separate .sh file and run that file instead of pasting both segments of the full code right into GeekTool like how I'm currently doing it. What do you think?
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^\MÍW^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