Solved

Reading, writing and substring operations in Shell Script

Posted on 2003-10-21
5
27,975 Views
Last Modified: 2013-12-13
I am new to shell scripts so please excuse my newbie questions or mistakes

1) How do i read a file into a shell script variable.?  While reading i should preserve all special characters, tabs, carriage returns etc, ineffect everything that is there in that file should be kept as is.  A text file needs to be created by this script which will further be processed by a Java program.

2) Once i read the file, i need to check if the first 2 characters are **.

3) How do i write to a new file everytime the script is executed.  The name is not very important.

Thank you all for your help
0
Comment
Question by:ns11
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 100 total points
ID: 9597572
>How do i read a file into a shell script variable.?  
for i in `cat filename`
or you can use
cat filename | while read line

>While reading i should preserve all special characters, tabs, carriage returns etc, ineffect everything that is there
>in that file should be kept as is.
first method I listed will not preserve newlines and other formatting while second one will

whether you should preserve them or not depends on your application needs

>A text file needs to be created by this script which will further be processed by a Java program.
why not use cp if you are simply copying it !!

>2) Once i read the file, i need to check if the first 2 characters are **.
use grep ^** or sed if you wish to replace them ... more details will be helpful

>3) How do i write to a new file everytime the script is executed.  The name is not very important.
a simple way could be to keep a count in a file ... increment this count everytime you write to a newfile
generate name as
newfile.<count from file>
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9604807
>3) How do i write to a new file everytime the script is executed.  The name is not very important.
for this purpose, you can also use mktemp ... man mktemp

any feedback ?
0
 
LVL 1

Expert Comment

by:Skytzo
ID: 9745345

There are two powerful utilties that you can use.

Sed and Awk.   I advise you to go to google and do a search on shell scripting tutorial and simply walk through the step by step guides that you find.

Second, go to google and do a search on Sed and Awk tutorial and work through those.

What you are trying to achieve is relatively simple, and could probably get what you need within a few hours of going through the tutorials.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 84
VB 6.0 printer how to align 6 57
topping3 challenge 14 69
How to get boolean result of md5sum from two files in powershell 8 71
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now