LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-04-2020, 08:24 PM   #1
questionsBot
Member
 
Registered: Jun 2020
Posts: 94

Rep: Reputation: Disabled
How can I edit files with a script? - Writing a "after install" script for my laptop


So I have been playing with BodHi for about a month now and really getting into it, but have yet to commit to it as a productive environment. I am still reinstalling. I have made a script to install all the software I have found I like and also set a lot of that software up... but one of the things I have to do myself is edit text files and stuff. Is there a way to edit text files with scripts? maybe searching and replacing a line, or adding to the bottom of the file or something? Or even making a new text file and adding lines to that?

Thanks!
 
Old 07-04-2020, 08:37 PM   #2
tinfoil3d
Member
 
Registered: Apr 2020
Location: Japan/RJCC
Distribution: debian, lfs, whatever else i need in qemu
Posts: 231

Rep: Reputation: Disabled
Of course... just an examples of what to look for:
Code:
echo "add the line at the end" >> /target/file
Code:
echo "This are a mistake" | sed 's/are/is/'
Code:
printf "line 1\nline2\n" | grep "line 2"
Google for "shell scripting", "bash scripts"
 
1 members found this post helpful.
Old 07-04-2020, 10:18 PM   #3
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 4,998

Rep: Reputation: 1493Reputation: 1493Reputation: 1493Reputation: 1493Reputation: 1493Reputation: 1493Reputation: 1493Reputation: 1493Reputation: 1493Reputation: 1493
Quote:
Originally Posted by questionsBot View Post
Is there a way to edit text files with scripts? maybe searching and replacing a line, or adding to the bottom of the file or something? Or even making a new text file and adding lines to that?
The short answer is yes, absolutely. The usual tools are sed and awk.

Substituting strings is the domain of sed:
Code:
sed s/oldstring/newstring/g original-file > new-file
sed -i s/oldstring/newstring/g original-file
oldstring can be a regular expression.

For more complex text processing, awk is very handy. It requires a minimum of programming skills. The following creates a quick and dirty histogram of all words in a file.
Code:
awk '{ for (i=1;i<NF;i++) histogram[$i]++ }
       END { for (word in histogram) print word ":", histogram[word] }' textfile
For more fun, see my signature.

Last edited by berndbausch; 07-04-2020 at 10:41 PM. Reason: removed an error from the awk program
 
Old 07-04-2020, 11:48 PM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,902
Blog Entries: 3

Rep: Reputation: 2443Reputation: 2443Reputation: 2443Reputation: 2443Reputation: 2443Reputation: 2443Reputation: 2443Reputation: 2443Reputation: 2443Reputation: 2443Reputation: 2443
One tip about using -i with sed is to have it save a backup of the original.

Code:
sed -i.orig -e 's/old/new/' file.txt
That will leave the original as file.txt.orig. See "man sed" for the details.

When writing whole files, you might consider using "HERE" documents.

Code:
$ cat <<EOT > x.txt
a 
b
c
EOT

$ cat x.txt 
a 
b
c
They are described in several of the major guides and tutorials, albeit maybe not as clearly as they could be:

https://wiki-dev.bash-hackers.org/syntax/redirection
http://www.tldp.org/LDP/abs/html/here-docs.html
https://linux.die.net/abs-guide/here-docs.html

However, once you get beyond editing simple text and start messing with JSON, EBNF, XML, or other weird formats, it will be much easier (and safer) to just replace the whole file. If you must edit in place then for those formats you cannot use AWK or sed scripts and must turn to perl or python instead.
 
Old 07-05-2020, 05:52 AM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 1,142

Rep: Reputation: Disabled
Recently found out there's an extension to gawk (yes, starting from version 4.1 gawk supports loadable modules!) that provides sed/perl/ruby -i functionality.
 
1 members found this post helpful.
  


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
Can not edit cupsd.conf and can't edit properties David J Linux - Newbie 1 08-17-2018 10:57 AM
[SOLVED] How can I edit "EPUB" and "Mobi" files via calibre? hack3rcon Linux - Software 1 02-15-2015 04:42 AM
[SOLVED] SED Multiline edit -- Quit After First Match/Edit RandyTech Linux - Newbie 9 06-09-2014 03:15 AM
Trying to edit a .spec file in vim (E212: Can't open files for writing) subharaj Linux - Software 1 02-14-2012 03:14 AM
Help With Java Problem Please"""""""""""" suemcholan Linux - Newbie 1 04-02-2008 06:02 PM

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

All times are GMT -5. The time now is 11:34 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
Open Source Consulting | Domain Registration