LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-26-2004, 12:09 PM   #1
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Rep: Reputation: 15
Unhappy bash script on manipulating files


I am a Linux newbie.
I wrote a shell script to handle several html files.

I have several questions:

1 - why this work
cat file.txt |sed -e 's/#/\n/'
(substitute every occurrence of '#'
with a LF)

... and this doesn't work?
cat file2.txt | sed -e 's/\//'
My goal was to get rid off all LF's...

I was obliged to revert to a vim macro:
vim -es file2.txt<m.vim

The content of the macro m.vim was:
:% s/\n//
:w
:q
This worked but now I need a further macro
(the big shell macro and the vim macro...)

2 - the output of the shell macro needs some
formatting...

Is it possible to load the output into
a variable ? or to be more detailed...

is there any mean to get the line length
of each line of a text file?

If it was possible to load a variable with
for example the 'cat file' output (for
a text file of only 1 line using head)...

from a string variable you can obtain the
length and then conditionally output one
or more tabs to format purposes...

3 -When I start working I open a console.
I adjust the shape of this console
running a script:

konsole --vt_sz 72x60 --nohist

Is there any method not so rude?
by means of configuration files, perhaps...?
 
Old 09-26-2004, 01:26 PM   #2
scottman
Member
 
Registered: Jul 2004
Location: USA
Distribution: Slackware, FreeBSD, LFS
Posts: 72

Rep: Reputation: 15
Hey fiomba,

Good to hear your learning scripting. Unfortunately, I can only help with some of your question, as some are in areas I haven't worked with, and I have to leave soon.
I can give you an idea of how I would trim newlines, but it doesn't use sed (I'm sure there's a way, but I find tr faster).

cat file.txt | tr -d "\n"

Check out the man page on tr, it's a pretty standard command, so it should be fairly portable.

If you wanted to put this into a variable, all you would need to do is set it to execute in a subshell, and have the variable equal that.

textvariable=$(cat file.txt | tr -d "\n")

By using the $( ) form, commands inside will be executed as shell commands before they are used in the script.

Hope these tidbits help you on your way, I know there is a command to count characters, but I've never used it in a script so I forgot it

Last edited by scottman; 09-26-2004 at 01:28 PM.
 
Old 09-26-2004, 02:00 PM   #3
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Original Poster
Rep: Reputation: 15
Thanks, scottman, you answered to 2 of my 3 questions
(the more important ones).

Your suggestions worked perfectly.

Last edited by fiomba; 09-26-2004 at 02:02 PM.
 
Old 09-26-2004, 02:56 PM   #4
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
count characters:
wc -c filename
or you can use stdin
wc -c < filename
etc...
echo $STRING | wc -c

Last edited by 95se; 09-26-2004 at 03:01 PM.
 
Old 09-27-2004, 05:05 AM   #5
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Original Poster
Rep: Reputation: 15
Thanks also to you, 95se;
your is an alternative method to
count lines.

In effect it has one step less, in
comparison to scottman's method
(load a variable with the line content and
obtain the string length).

To better thank for the help you all gave me,
I am considering to share my findings
(I am a Linux newbie but
not a programmer newbie).

The object of my findings is the manipulation
of html files but you can generalize.
Of course all is turned to newbies (like me),
and not to experts...
But I need some encouragement,
if someone is interested...
 
Old 09-27-2004, 08:16 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Re: bash script on manipulating files

Quote:
Originally posted by fiomba 1 - why this work
cat file.txt |sed -e 's/#/\n/'
(substitute every occurrence of '#' with a LF)
Actually it won't work: it will substitute the first occurence only for each pattern. you have to replace 's/#/\n/' with 's/#/\n/g'.
Quote:
... and this doesn't work?
cat file2.txt | sed -e 's/\//'
My goal was to get rid off all LF's...
sed can't do that. \n is not recognized by sed in patterns... Side-note: the 'n' is missing from your pattern. But scottman's suggestion to use tr is excellent. I'm glad I learned it!
Quote:
I was obliged to revert to a vim macro:
vim -es file2.txt<m.vim
The content of the macro m.vim was:
:% s/\n//
:w
:q
This worked but now I need a further macro (the big shell macro and the vim macro...)
You could try that:
vim -es file2.txt <<-END
:% s/\\n//
:w
:q
END
Quote:
is there any mean to get the line length of each line of a text file?
Maybe that:
cat file.txt | while read line; do echo "$line" | wc -L; done >result.txt

Yves.
 
Old 09-27-2004, 09:23 AM   #7
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Original Poster
Rep: Reputation: 15
I know the use of sed subsitution.
In my case there is only one '#' per line and so I don't
need the "global " g.

Instead your example of vim macro without a macro file
is very interesting (I did not know this technique!).
Also you made a mistake (:% s/\\n//) instead of ":% s/\n//"
but in your case it works the same!

For the last point (length of each line) in my case I don't need
a loop because only one line pass through the filter:
cat file.txt | grep 'something' | wc -c

Thank you 'theYinYeti', especially for the vim part !

P.S. As far as my question about the size of the consolle (n.3)
is concerned, I have already found the answer (in Internet).
 
Old 09-28-2004, 09:07 AM   #8
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Oh? I thought \\ would be needed because bash interprets what gets written between <<-KEYWORD and KEYWORD. So I feared that "\n" would simply become "n". For example, if you want to put "$PATH" in your stdin, and not "/bin:/usr/bin:...", the you need to write "\$PATH".

Yves.
 
Old 10-30-2004, 08:31 PM   #9
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
To get lengths of the lines, compact version:
Code:
cat foo | awk '{print length($0)}'
 
  


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
manipulating files tableyou Linux - Newbie 8 11-08-2005 02:38 AM
bash script to delete files c0d3 Programming 9 12-05-2004 10:45 PM
Identifying files in bash script Cruger Programming 7 03-15-2004 10:49 AM
Manipulating Files in vi Baldorg Linux - General 1 09-19-2003 07:25 PM
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 07:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:36 AM.

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
Open Source Consulting | Domain Registration