0

I have a question for you, do you know if there is a program/software to add easily a character to all lines? For example, if I want to add + on front on those lines and don't want to do that one by one, is it possible?

+CONFIG_PACKAGE_git=y
+CONFIG_PACKAGE_git-http=y
+CONFIG_PACKAGE_htop=y
CONFIG_PACKAGE_https-dns-proxy=y
CONFIG_PACKAGE_ip-full=y
CONFIG_PACKAGE_ip6tables-nft=y
CONFIG_PACKAGE_iperf3=y
CONFIG_PACKAGE_iptables-mod-conntrack-extra=y
CONFIG_PACKAGE_iptables-mod-ipopt=y
CONFIG_PACKAGE_iptables-nft=y
CONFIG_PACKAGE_irqbalance=y
CONFIG_PACKAGE_kmod-crypto-authenc=y
CONFIG_PACKAGE_kmod-crypto-cbc=m
CONFIG_PACKAGE_kmod-crypto-cts=m
CONFIG_PACKAGE_kmod-crypto-deflate=m
CONFIG_PACKAGE_kmod-crypto-des=m
CONFIG_PACKAGE_kmod-crypto-lib-chacha20=y
CONFIG_PACKAGE_kmod-crypto-lib-chacha20poly1305=y
CONFIG_PACKAGE_kmod-crypto-lib-curve25519=y
CONFIG_PACKAGE_kmod-crypto-lib-poly1305=y
CONFIG_PACKAGE_kmod-crypto-md5=m
CONFIG_PACKAGE_kmod-crypto-sha1=m
CONFIG_PACKAGE_kmod-crypto-sha512=m
CONFIG_PACKAGE_kmod-crypto-xts=m
CONFIG_PACKAGE_kmod-cryptodev=y
CONFIG_PACKAGE_kmod-ifb=y
CONFIG_PACKAGE_kmod-ip6tables=y
CONFIG_PACKAGE_kmod-ipt-conntrack=y
CONFIG_PACKAGE_kmod-ipt-conntrack-extra=y
CONFIG_PACKAGE_kmod-ipt-core=y
CONFIG_PACKAGE_kmod-ipt-ipopt=y
CONFIG_PACKAGE_kmod-ipt-ipset=y
CONFIG_PACKAGE_kmod-lib-zlib-deflate=m
CONFIG_PACKAGE_kmod-lib-zlib-inflate=m
# CONFIG_PACKAGE_kmod-mwifiex-sdio is not set
CONFIG_PACKAGE_kmod-nf-conncount=y
CONFIG_PACKAGE_kmod-nf-conntrack-netlink=y
CONFIG_PACKAGE_kmod-nf-ipt=y
CONFIG_PACKAGE_kmod-nf-ipt6=y
CONFIG_PACKAGE_kmod-nf-nat6=y
CONFIG_PACKAGE_kmod-nft-compat=y
CONFIG_PACKAGE_kmod-sched-cake=y
CONFIG_PACKAGE_kmod-sched-connmark=y
CONFIG_PACKAGE_kmod-sched-core=y
CONFIG_PACKAGE_kmod-tun=y
CONFIG_PACKAGE_kmod-udptunnel4=y
CONFIG_PACKAGE_kmod-udptunnel6=y
CONFIG_PACKAGE_kmod-wireguard=y
CONFIG_PACKAGE_libatomic=y
CONFIG_PACKAGE_libattr=y
CONFIG_PACKAGE_libavahi-client=y
CONFIG_PACKAGE_libavahi-dbus-support=y
CONFIG_PACKAGE_libbpf=y
CONFIG_PACKAGE_libbz2=y
5
  • 2
    Which text editor?
    – harrymc
    Mar 3, 2023 at 15:04
  • 2
    I've seen many questions like this (about programmatically modifying a text). Even if you find a text editor which can do this next time the problem is a little different and you will struggle again. That is why I recommand learning a scripting language like PHP or python. For example in PHP this code does what you want and it's only one line: file_put_contents("out.txt", "+".str_replace("\n", "\n+", file_get_contents("in.txt")));
    – zomega
    Mar 3, 2023 at 16:18
  • 1
    even batch (the lowest of the low) can make quick work of this with a simple for loop. Mar 3, 2023 at 21:35
  • which operating system? In a linux, unix (e.g. Mac OSx) platform this would easily be done using a single command with a tool like "sed" at the command line or in a script. In windows things are a little more complex
    – Geeky51
    Mar 3, 2023 at 22:38
  • 1
    @James78 - this feature was first known to me as "Column Edit" in Ultra Edit, but most programs refer to it now as "Block Edit" or "Multi Caret Edit". If you are looking for this functionality in a text editor, check for those named features.
    – StingyJack
    Mar 6, 2023 at 2:26

5 Answers 5

7

Yes, there are many, many ways to do this depending on what you are looking for.

sed -i -E 's/^(.*)$/\+\1/' file.txt

Would add a + in front of each line in file.txt This literally translates to:

for each line in file.txt
    take the passage of any characters between the beginning and end of the line
    and replace it with the same passage with a + prepended

As pointed out in the comments this could be minimized to:

sed -i 's/^/+/' file.txt

Which translates to Replace the beginning of every line with a +

If you want to be a bit more selective and interactive the I edit operation in vim starts editing at the beginning of the line. Combine that with . for repeat last edit here. Doing that you can add a plus to one line, hit escape, and then you can just arrow down to additional lines hitting . on each no matter where on the line you arrow to to begin that line with a +.

2
  • 5
    Why not simply 's/^/+/'? (You don't even need -E then either.)
    – DonHolgo
    Mar 4, 2023 at 13:39
  • That should work. I was going for a literal "Take everything from the beginning to the end of the line in each line and add a + to the front" instead of a minimized "convert the front of every line into a +.
    – davolfman
    Mar 6, 2023 at 17:45
7

Use something like Notepad++ or Sublime Text.

Put your cursor at the start of the first line (line 1, column 1). Hold Shift + Alt, then click at the start of the last line (line 54, column 1). You should see a flashing cursor that spans all the lines, on the left border.

Now press +, and the keypress will be repeated across all the selected lines.

This works in most text editors & in SQL Server Management Studio, but won't work in regular Notepad.exe or Wordpad.exe (native Windows text editors).

Also see https://techcommunity.microsoft.com/t5/core-infrastructure-and-security/quick-tip-shift-alt-for-multiple-line-edits/ba-p/371355

1
  • spikey_richie . I will try your suggestion, seems pretty good and not too much complicated. Btw, someone gave me that link, it seems pretty easy too, but I wonder if I can choose which section of text must have the characters....? [link]linuxconfig.org/…
    – James78
    Mar 3, 2023 at 20:23
5

Most good text editors that are designed for working with source code (Nodepad++, Sublime Text, VSCode, Vim, EMACS, etc) have robust search and replace syntax that allows matching on the start of a line as part of the search expression. This is part of what is known as support for regular expressions in search patterns.

In most such editors, a simple ^ as the search pattern and a + as the replacement text will do exactly what you have described in your question (so, for example, in Vim it would be :%s/^/+/).

However, what you probably want (only add the plus to lines that aren’t empty, and don’t start with a # or +) is rather trickier, and the exact syntax varies a bit. For VSCode you would want ^([^#+]) for the search pattern and +$1 as the replacement text. For Vim it would instead (usually) be :%s/^\([^#+]\)/+\1/. Other editors may have their own syntax for this.

2

Python is not hard to learn, the oneliner below uses a list comprehension to read lines from stdin and print them out (to stdout), after using a formatstring for adding the '+' before the text on a line.

$ cat z.txt
Read 
my 
lips

$ python -c "import sys;[print(f'+{line}',end='') for line in sys.stdin]" <z.txt

The corresponding .py file:

$ cat z.py
#!/bin/env python

import sys

for line in sys.stdin:
  print(f'+{line}',end='')

The result is the same from both:

$ chmod 755 z.py # Once, to set the script be executable
$ ./z.py <z.txt
+Read
+my
+lips

Ref:
https://www.google.com/search?q=python+list+comprehension
https://www.google.se/search?q=python+format+string

Add regular expressions to this, e.g. import re and you will have all functions that Notepad++ provides and more than that:
e.g. rename files named image(1).jpg, image(10).jpg and image(100).jpginto names patterned by MyImage2023-03-03_0001.jpg in a breeze.

How? Here is a basic start for that script:

$ cat reformat_name.py 
#!/bin/env python
import sys, re

for line in sys.stdin:
  z=re.match('^(.*)\((.*)\)(.*)$',line)
  print( '{}{:>05}{}'.format(z.group(1),z.group(2),z.group(3) ) )

$ cat reformat_name.txt 
image(1).jpg
image(10).jpg
image(100).jpg

$ chmod 755 z.py  # Once, to set the script be executable
$ ./reformat_name.py <reformat_name.txt 
image00001.jpg
image00010.jpg
image00100.jpg

$ 

Ref: https://www.google.com/search?q=python+regular+exp

1

Quick and dirty... Excel and Notepad

enter image description here

Highlight the TAB character, replace it to nothing:

enter image description here

Final Results:

enter image description here

PS: I'm a prorgrammer. I use Notepad++. I also know extensive shell scripting, knows RegEx, Python, PHP, NoSQL, SQL, React, Java, JavaScript, blah blah blah.

Sometimes you just need to use what you got.

1
  • This would also work with =concat() Mar 4, 2023 at 21:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .