| Author |
Message |
don570

Joined: 10 Mar 2010 Posts: 2457 Location: Ontario
|
Posted: Tue 03 Apr 2012, 13:21 Post subject:
Add text to head of file |
|
I find it useful to put text at the head rather than the tail of a file
stored on a hard drive.
Here's a script that you can read and it should be self-explanatory.
| Code: | #!/bin/sh
# SCRIPT SHOWS HOW TO ADD LINES TO HEAD OF FILE
# first create a file
echo "Here is data
in file." > DATA_file
# then create the text to add to top of file
TEXT=`echo -e "I WANT THIS TEXT \nON TOP \n \n "`
# now add the two together
cat - DATA_file <<<"$TEXT" > /tmp/temp_script_data
mv -f /tmp/temp_script_data DATA_file
# show resulting file with default editor
defaulttexteditor DATA_file
exit 0
|
The output is shown in this image...
_________________________________________________
|
|
Back to top
|
|
 |
don570

Joined: 10 Mar 2010 Posts: 2457 Location: Ontario
|
Posted: Tue 03 Apr 2012, 13:28 Post subject:
|
|
Here is how to manipulate the text
| Code: |
\\
backslash
\a
alert (BEL)
\b
backspace
\c
suppress trailing newline
\f
form feed
\n
new line
\r
carriage return
\t
horizontal tab
\v
vertical tab |
|
|
Back to top
|
|
 |
don570

Joined: 10 Mar 2010 Posts: 2457 Location: Ontario
|
Posted: Sat 28 Apr 2012, 12:08 Post subject:
add text to variable's contents |
|
add text to variable's contents
I realised that there is another way to add text to the head.
This time the text is added to beginning of each line
| Code: | #!/bin/sh
# Assign text to a variable
TEXT=`echo -e "why this \n is my text\n line."`
#create some text to put at beginning
FIRST=First_
echo -e "$TEXT" | sed 's/^/'$FIRST'/'
|
and the terminal output is
| Code: | First_why this
First_ is my text
First_ line. |
_______________________________________________
|
|
Back to top
|
|
 |
don570

Joined: 10 Mar 2010 Posts: 2457 Location: Ontario
|
Posted: Mon 30 Apr 2012, 12:58 Post subject:
|
|
When there is whitespace in the text that is added
there needs to be a change of IFS to some other character
such as colon
IFS=":"
| Code: | #!/bin/sh
# Assign text to a variable
TEXT=`echo -e "why this \n is my text\n line."`
#create some text to put at beginning
FIRST="First attempt"
IFS=":"
echo -e "$TEXT" | sed 's/^/'$FIRST'/'
|
Terminal output
| Code: | First attemptwhy this
First attempt is my text
First attempt line. |
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Tue 01 May 2012, 16:52 Post subject:
|
|
| Code: | | sed -i '1s/^/hello\n/' file |
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
Flash
Official Dog Handler

Joined: 04 May 2005 Posts: 9841 Location: Arizona USA
|
Posted: Tue 01 May 2012, 23:48 Post subject:
|
|
Is this thread at all related to how metadata is added to a mp3 file for instance?
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Wed 02 May 2012, 01:34 Post subject:
|
|
id3 tags aren't all at the head ... I did post a little bash a while back on stack overflow (or was it superuser?) to grok out the tags for name/artist though for file renaming purposes ... just involved something like grepping for TAG ... not sure about using sed to replace it though - why do you ask?
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
vovchik

Joined: 23 Oct 2006 Posts: 1229 Location: Ukraine
|
Posted: Wed 02 May 2012, 06:35 Post subject:
|
|
Dear puppians,
If you have coreutils installed, you can always use "tac" instead of cat. It reads and displays starting from the end of a file.
As for id3 tags, and if you can program in c, c++ or bacon, you can always use "libtag_c.so" for version 1 tags. I have included a little bacon example. You can also write tags using this lib.
With kind regards,
vovchik
| Description |
|

Download |
| Filename |
lsmp3.bac.tar.gz |
| Filesize |
1.61 KB |
| Downloaded |
165 Time(s) |
|
|
Back to top
|
|
 |
Flash
Official Dog Handler

Joined: 04 May 2005 Posts: 9841 Location: Arizona USA
|
Posted: Wed 02 May 2012, 19:41 Post subject:
|
|
| technosaurus wrote: | | .... - why do you ask? |
I'm just trying to understand how the tags work so maybe I can figure out why every mp3 player seems to display them differently.
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Wed 02 May 2012, 20:29 Post subject:
|
|
http://superuser.com/questions/398712/settingid3-title-tag-to-be-equal-to-mp3-file-name
@flash the above link has my code, but there is actually a couple of specs for them which different id* libs implement @ differing levels
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
don570

Joined: 10 Mar 2010 Posts: 2457 Location: Ontario
|
Posted: Sat 05 May 2012, 13:15 Post subject:
|
|
To add the text just to the first line
as small modification to sed command is necessary
| Code: |
#!/bin/sh
# Assign text to a variable
TEXT=`echo -e "why this \n is my text\n line."`
#create some text to put at beginning
FIRST="First attempt"
IFS=":"
echo -e "$TEXT" | sed '1s/^/'$FIRST'/'
|
The terminal output ---->
| Code: | First attemptwhy this
is my text
line. |
_____________________________________________________
|
|
Back to top
|
|
 |
don570

Joined: 10 Mar 2010 Posts: 2457 Location: Ontario
|
Posted: Sat 02 Jun 2012, 13:45 Post subject:
|
|
Instead of adding the text to the begining of the first line
I made two small changes to put the text at
the end of the second line.
Note that the dollar sign represents the end of the line.
| Code: | #!/bin/sh
# Assign text to a variable
TEXT=`echo -e "why this \n is my text \n line."`
#create some text to put at beginning
FIRST="First attempt"
IFS=":"
echo -e "$TEXT" | sed '2s/$/'$FIRST'/'
|
Here is the result in the terminal
| Code: | why this
is my text First attempt
line. |
|
|
Back to top
|
|
 |
seaside
Joined: 11 Apr 2007 Posts: 832
|
Posted: Sun 03 Jun 2012, 18:37 Post subject:
|
|
don570,
Very handy items.
You could also do it without a change to the IFS this way -
| Code: | | echo -e "$TEXT" | sed "2s/$/$FIRST/" |
Cheers,
s
|
|
Back to top
|
|
 |
don570

Joined: 10 Mar 2010 Posts: 2457 Location: Ontario
|
Posted: Wed 06 Jun 2012, 18:55 Post subject:
|
|
Thanks.Good tip!
When I removed IFS=":"
I got the following error
| Code: | sed: -e expression #1, char 10: unterminated `s' command
Script completed hit RETURN to close window. |
So your suggestion works with the double quotes
rather than single quotes.
| Code: | #!/bin/sh
# Assign text to a variable
TEXT=`echo -e "why this \n is my text \n line."`
#create some text to put at ending
FIRST="First attempt"
echo -e "$TEXT" | sed "2s/$/$FIRST/"
|
______________________________________________
|
|
Back to top
|
|
 |
seaside
Joined: 11 Apr 2007 Posts: 832
|
Posted: Thu 07 Jun 2012, 15:23 Post subject:
|
|
don570,
Yes, sed and variables can be tricky.
Since, we are both fans of right-click menus, here's one you might wish to add to your collection of right-click menu items
When downloading files, many times a specific md5sum.txt file is not supplied, but instead the md5sum appears on the website page.
In those cases, it's handy to simply highlight the md5sum on the webpage which puts the selection on the selection clipboard and after the file is downloaded, just right-click the file with "md5sum-check" for verification.
Here's the script for "md5sum-check"
Cheers,
s
| Code: | #!/bin/sh
# md5sum-check
# link to /root/.config/rox.sourceforge.net/OpenWith/md5sum-check
# md5sum check from clip
# highlight md5sum for clip selection
# right-click this script on file
# Seaside June 6 2012
CLPSUM=`xclip -o`
MD5SUM="$CLPSUM $1"
[[ "`md5sum -c <<<"$MD5SUM"`" ]] && Xdialog --ok-label "Passed" --backtitle "Md5sum Check" --msgbox "YES \n$1 \nMd5sum \nOK" 0 0 || Xdialog --ok-label "Failed" --backtitle "Md5sum Check" --msgbox "NO \n$1 \nMd5sum \nFailed" 0 0
|
|
|
Back to top
|
|
 |
|