Add text to head of file

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

Add text to head of file

#1 Post by don570 »

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: Select all

#!/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...

Image

_________________________________________________

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#2 Post by don570 »

Here is how to manipulate the text

Code: Select all


\\
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

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

add text to variable's contents

#3 Post by don570 »

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: Select all

#!/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: Select all

First_why this 
First_ is my text
First_ line.
_______________________________________________

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#4 Post by don570 »

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: Select all

#!/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: Select all

First attemptwhy this 
First attempt is my text
First attempt line.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#5 Post by technosaurus »

Code: Select all

sed -i '1s/^/hello\n/' file
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#6 Post by Flash »

Is this thread at all related to how metadata is added to a mp3 file for instance?

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#7 Post by technosaurus »

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?
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#8 Post by vovchik »

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
Attachments
lsmp3.bac.tar.gz
(1.61 KiB) Downloaded 341 times

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#9 Post by Flash »

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. :x

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#10 Post by technosaurus »

http://superuser.com/questions/398712/s ... -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
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#11 Post by don570 »

To add the text just to the first line
as small modification to sed command is necessary

Code: Select all

#!/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: Select all

First attemptwhy this 
 is my text
 line.
_____________________________________________________

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#12 Post by don570 »

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: Select all

#!/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: Select all

why this 
 is my text First attempt
 line.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#13 Post by seaside »

don570,

Very handy items.

You could also do it without a change to the IFS this way -

Code: Select all

 echo -e "$TEXT" | sed  "2s/$/$FIRST/" 
Cheers,
s

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#14 Post by don570 »

Thanks.Good tip!

When I removed IFS=":"
I got the following error

Code: Select all

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: Select all

#!/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/"
______________________________________________

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#15 Post by seaside »

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: Select all

 #!/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
  

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

xclip

#16 Post by don570 »

Xclip needs to be installed to use that script.

http://murga-linux.com/puppy/viewtopic. ... 29&t=26887

______________________________________________

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

Re: xclip

#17 Post by seaside »

don570 wrote:Xclip needs to be installed to use that script.
[_
don570,

I think most puppies have xclip installed by default.

Cheers,
s

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#18 Post by don570 »

The puppies I use don't have xclip installed :cry:

I made a right click package using your ideas

http://murga-linux.com/puppy/viewtopic. ... 965#633965

Image

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#19 Post by seaside »

don570 wrote:The puppies I use don't have xclip installed :cry:

I made a right click package using your ideas
don570,

Yes, I guess a few don't have xclip.

Great! That addon right click item looks very nice.

Regards,
s
(There can never be too many right click actions. Right? :D )

Post Reply