Create text file with content in Linux

Hi Guys,

Need you help in creating a text file with content in linux directory.

Thanks in advance!

Sample text file called my.repo with the ff content below:

[el6]
name=el6
baseurl=http://myip/el6
enabled=1

you can use appendifle syntax to create a file first and then move to your Linux path , at the end you can update your path to Linux instead of windows.

Below is the sample example to create a file :-

appendfile {“System:” & name of operating system}
appendfile {“Computer:” & computer name}
appendfile {“DNS name:” & dns name}
delete "c:\log\properties.log"
copy __appendfile “c:\log\properties.log”

You use actionscirpt’s File Commands. There’s and explanation of the commands and simple examples for each in the linked documents.
You can use createfile until instead of appendfile if you’re writing multiple lines.

1 Like

Thanks gearoid!

What if I just need to change specific line of text in text file?

Creating a file is easy, editing a file is much harder though you could probably parse it with some relevance and re-create it

Could you provide us with a sample text file (including it’s location in the filesystem), and the changes you would like made?

Hi Aram,

File location in Linux /etc/yum.repos.d/pst.conf

Content:

[main]
enabled=1

Need change to:

[main]
enabled=0

How about something like the following actionscript:

delete __appendfile

// create an appendfile with all lines except that starting with 'enabled=1'
appendfile {concatenation "%0A" of lines whose (it as string as lowercase does not start with "enabled=1") of file "/etc/yum.repos.d/pst.conf"}
// add enabled=0 to end of appendfile
appendfile enabled=0

delete /etc/yum.repos.d/pst.conf.bak
move /etc/yum.repos.d/pst.conf /etc/yum.repos.d/pst.conf.bak
move __appendfile /etc/yum.repos.d/pst.conf

You would probably also want to include relevance checking for the ‘enabled=1’ condition prior to execution.

1 Like

This would work of course only if the line was the last line in the file. Otherwise you would have to skip the [main] line as well.

The ‘enabled’ line doesn’t have to be the last line per the above, but it would be added back to the end of the file, yes. If the line must be modified “in place”, there are methods to do that as well.

And it replaces all instances of enabled=1. As @Aram says if you need to get more specific or change the line in place we can give you actionscript to do that too, it’s just a little more involved.

As it looks like the file to be changed is an INI file the header ( shown as [main] ) would have to be right above the changed value so if the INI is more complex the actionscript definitely needs to be more complex

Here’s a sample with in-line modification:

delete __appendfile

// grab all lines prior to "enabled=1"
appendfile {concatenation "%0A" of lines (integers to (line number of lines whose (it as string as lowercase contains "enabled=1") of it - 1)) of file "/etc/yum.repos.d/pst.conf"}
// insert new desired line to replace previous iteration
appendfile enabled=0
// grab all lines after "enabled=1"
appendfile {concatenation "%0A" of lines (integers in (line number of lines whose (it as string as lowercase contains "enabled=1") of it + 1, number of lines of it)) of file "/etc/yum.repos.d/pst.conf"}

delete /etc/yum.repos.d/pst.conf.bak
move /etc/yum.repos.d/pst.conf /etc/yum.repos.d/pst.conf.bak
move __appendfile /etc/yum.repos.d/pst.conf
3 Likes

Working in windows, will modify and test in linux.

// file location (E:\Logs\pst.txt")
parameter "filename" = "{pathname of file ("E:\Logs\pst.txt") whose (exists lines whose (it contains "enabled=1")of it )}"

parameter "textToReplace" = "enabled=1"
parameter "newText" = "enabled=0"

// iterate through the file replacing lines as necessary
appendfile {concatenation "%0d%0a" of ( if (it contains (parameter "textToReplace")) then ((preceding text of first (parameter "textToReplace") of it) & (parameter "newText") & (following text of first (parameter "textToReplace") of it) ) else it ) of lines of file (parameter "filename")}

// backup old file
move "{parameter "filename"}" "{parameter "filename"}.bak"
// replace with the new file
move __appendfile "{parameter "filename"}"
1 Like

Thanks everyone for sharing your expertise and helping me to sort it out!

Hi Aram,

Here’s the result of your sample script.

Completed delete __appendfile
Completed // grab all lines prior to “enabled=1”
Failed appendfile {concatenation “%0A” of lines (integers to (line number of lines whose (it as string as lowercase contains “enabled=1”) of it - 1)) of file “/etc/yum.repos.d/ibm-repository.conf”}
// insert new desired line to replace previous iteration
appendfile enabled=0
// grab all lines after “enabled=1”
appendfile {concatenation “%0A” of lines (integers in (line number of lines whose (it as string as lowercase contains “enabled=1”) of it + 1, number of lines of it)) of file “/etc/yum.repos.d/ibm-repository.conf”}
delete /etc/yum.repos.d/pst.conf.bak
move /etc/yum.repos.d/pst.conf /etc/yum.repos.d/ibm-repository.conf.bak
move __appendfile /etc/yum.repos.d/ibm-repository.conf

Attached is the result of my script.

The appendfile (lines 8, 9 and 10) should be all on a single line

1 Like

Will try that. Thanks AlanM!

Here is the result.