Page 1 of 1

Add text to head of file

Posted: Tue 03 Apr 2012, 17:21
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

_________________________________________________

Posted: Tue 03 Apr 2012, 17:28
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

add text to variable's contents

Posted: Sat 28 Apr 2012, 16:08
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.
_______________________________________________

Posted: Mon 30 Apr 2012, 16:58
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.

Posted: Tue 01 May 2012, 20:52
by technosaurus

Code: Select all

sed -i '1s/^/hello\n/' file

Posted: Wed 02 May 2012, 03:48
by Flash
Is this thread at all related to how metadata is added to a mp3 file for instance?

Posted: Wed 02 May 2012, 05:34
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?

Posted: Wed 02 May 2012, 10:35
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

Posted: Wed 02 May 2012, 23:41
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

Posted: Thu 03 May 2012, 00:29
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

Posted: Sat 05 May 2012, 17:15
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.
_____________________________________________________

Posted: Sat 02 Jun 2012, 17:45
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.

Posted: Sun 03 Jun 2012, 22:37
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

Posted: Wed 06 Jun 2012, 22:55
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/"
______________________________________________

Posted: Thu 07 Jun 2012, 19:23
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
  

xclip

Posted: Mon 11 Jun 2012, 17:52
by don570
Xclip needs to be installed to use that script.

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

______________________________________________

Re: xclip

Posted: Mon 11 Jun 2012, 19:32
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

Posted: Wed 13 Jun 2012, 21:08
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

Posted: Wed 13 Jun 2012, 21:59
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 )