LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-28-2011, 12:05 PM   #1
LoDam
LQ Newbie
 
Registered: Jan 2011
Location: Fr
Distribution: ArchLinux at the moment
Posts: 25

Rep: Reputation: 4
Bash - add text at the begining of a line


Hello LQ,

A very simple question for you:

Here is the situation. I have a lot a folders, each named by a number, and in each of these folders I have a specific file (stddev.dat) containing a single line (of numbers)

I need to have a single file with each line being one of the stddev.dat (no matter if it is sorted or not), and also I need to add at the begining of each line the number of the folder it comes from.

I 'm no bash expert, and the "add at the begining of the line" is a bit of problem to me".

Here is what I've come up with so far, just to put everything in one file, can you help me with the rest (and also if you know a better/more elegant way to do the same thing I've done, I'm listening )

Code:
#!/bin/bash 
  2 LIST=$(ls) 
  3  
  4 for folder in $LIST 
  5 do 
  6     if [ -d $folder ] 
  7         then 
  8             cd $folder 
  9             cat stddev.dat >> ../stddev 
 10            cd .. 
 11     fi 
 12 done
 
Old 02-28-2011, 01:06 PM   #2
Stephen Morgan
Member
 
Registered: Feb 2011
Location: England
Distribution: Slackware
Posts: 163

Rep: Reputation: 17
#!/bin/bash
LIST=$(ls)

for folder in "$LIST"; do
if [ -d "$folder" ]; then
cd "$folder"
sed 's/^.*/^'"$folder"'&/1' < stddev.dat >> ../stddev
cd ..
fi
done

# I think that'll work.
 
Old 02-28-2011, 01:37 PM   #3
LoDam
LQ Newbie
 
Registered: Jan 2011
Location: Fr
Distribution: ArchLinux at the moment
Posts: 25

Original Poster
Rep: Reputation: 4
Thank you.
Could you explain the sed command? I don't understand the &1 < stddev.dat part.

Otherwise I came up with a solution without sed (I don't like using solutions I don't really understand ^^)

Code:
#!/bin/bash 
  2 LIST=$(ls)
  3 
  4 if [ -f "stddev.dat" ]
  5     then
  6     rm stddev.dat
  7 fi
  8 
  9 for folder in $LIST
 10     do
 11         if [ -d $folder ]
 12             then
 13             cd $folder
 14             line=$(cat stddev.dat)
 15             line=$folder" "$line
 16             echo $line >> ../stddev.dat
 17             cd ..
 18         fi
 19     done
That was quitte simple actually!

Cheers,
Loic.
 
Old 02-28-2011, 03:56 PM   #4
Stephen Morgan
Member
 
Registered: Feb 2011
Location: England
Distribution: Slackware
Posts: 163

Rep: Reputation: 17
I'm no expert on sed, but '^' denotes the start of the line, '&' the string searched for, '.' any character and '*' anything, or nothing. So s/^.*/today&/ ought to substitute the start of the line followed by at least one character (so as to ignore blank lines), replacing it with the same thing but with "today" in front of it. The one at the end, I think, means to only do it once, but I'm not so sure about that. As all your files are meant to have one line, I believe you said, that would do the trick but be unneccessaru./

Code:
grim@21:54:16:~$ echo woof | sed 's/^.*/today&/1'
todaywoof
The '<' sign pipes the file into the command. You are assigning a variable and reading the contents of the file into it with the cat command. Say you wanted to replace all the 'a's in a one line file with 'b's, you could do it like this:

cat file.in | sed 's/a/b/g' >file.out

or like this:

sed 's/a/b/g' < file.in >> file.out

Or by assigning a variable:

var="$(cat file.in)"
echo "$var" | sed 's/a/b/g' >> file.out

All do the same thing.
 
1 members found this post helpful.
Old 02-28-2011, 04:02 PM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 45
Quote:
Originally Posted by Stephen Morgan View Post
#!/bin/bash
LIST=$(ls)

for folder in "$LIST"; do
if [ -d "$folder" ]; then
cd "$folder"
sed 's/^.*/^'"$folder"'&/1' < stddev.dat >> ../stddev
cd ..
fi
done

# I think that'll work.
don't have to specifically substitute the whole line. s/^/ word/ will do
 
Old 03-01-2011, 12:26 AM   #6
LoDam
LQ Newbie
 
Registered: Jan 2011
Location: Fr
Distribution: ArchLinux at the moment
Posts: 25

Original Poster
Rep: Reputation: 4
Thank's stephen morgan, cristal clear now!
 
Old 03-01-2011, 03:43 AM   #7
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Rep: Reputation: 49
Try this
Code:
find . -type f -iname 'stddev.dat' -printf "\n%h:" -exec 'cat' '{}' ';'
Assuming you are currently in folder containing all subfolders.

Last edited by sumeet inani; 03-01-2011 at 04:00 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] can add a text line to text files? windstory Linux - Newbie 4 08-25-2009 06:40 AM
Bash: How to add a line on top of text files guest Programming 12 02-07-2009 06:23 AM
How to add a text to first line of a text file? oskeewow Linux - Newbie 6 04-23-2008 12:40 PM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM


All times are GMT -5. The time now is 08:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration