LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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-03-2015, 04:00 PM   #1
weezy99
LQ Newbie
 
Registered: Feb 2015
Posts: 2

Rep: Reputation: Disabled
Question Tips on using bash script to edit a windows text file


I have a bash script executing on an AIX server to a windows share directory to update a windows text file. Most of the file is edited as expected but I have 3 issues that I am not sure how to resolve.

1. Windows text file contains a double backslash (\\) and the \\ is being written back as a single \ which corrupts the file.
2. I am not getting the last line of text in the file written back out.
3. Some lines of text are not written back out completely.

Here is a snippet of my bash script:
cp $Directory/Somefile $Directory/Somefile.$(date +'%Y%m%d')
cat /dev/null > ${HOME}/temp.out

while read -r inpLine; do
patternFound=n
echo ${inpLine} | grep -q 'SOMEPATTERN='
if [ $? = 0 ]; then
patternFound=y
echo "##${inpLine}" >> ${HOME}/temp.out
echo "SOMEPATTERN=newValue" >> ${HOME}/temp.out
fi

if [ ${patternFound} = "n" ]; then
echo ${inpLine} >> ${HOME}/temp.out
fi

done < $Directory/Somefile
mv ${HOME}/temp.out $Directory/Somefile
 
Old 02-03-2015, 04:51 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by weezy99 View Post
1. Windows text file contains a double backslash (\\) and the \\ is being written back as a single \ which corrupts the file.
In UNIX the backslash is one of the escape chars so if you need one as literal backslash you have to escape it:
Code:
 ~]$ echo "\\\\"
\\
 ~]$ echo "\\\\\\"
\\\

Quote:
Originally Posted by weezy99 View Post
2. I am not getting the last line of text in the file written back out.
Might be a line ending issue?


Quote:
Originally Posted by weezy99 View Post
3. Some lines of text are not written back out completely.
Running your script with the appropriate verbose, trace and or debug switches for your shell will help you understand how lines are executed.
 
Old 02-03-2015, 05:46 PM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Maybe a stupid idea, but running dos2unix on the file before editing the file and unix2dos afterwards might solve some compatibility problems.

Awk is better suited for this task, but if this project is only one of a kind it might not be worth the effort to learn it.

And please post code snippets in [.CODE][./CODE] tags.

jlinkels
 
Old 02-11-2015, 08:09 AM   #4
weezy99
LQ Newbie
 
Registered: Feb 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Sorry I am new to forum and didn't read the rules about how to appropriately post code snippets. Also very new to AIX/Linux and bash.
The resolution to my issue was to change from echo approach to sed and handle the special characters.
 
  


Reply



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
How To Edit a File Through Bash Script rabir Linux - Newbie 4 04-24-2012 03:37 AM
[SOLVED] bash script to edit file seeberg Programming 4 04-05-2012 11:17 AM
[SOLVED] Bash Script. How to edit a file? y0_gesh Programming 3 02-28-2012 05:52 AM
Bash script to edit text file snowman81 Linux - Desktop 2 01-10-2007 03:33 PM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:02 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