Sponsored Content
Full Discussion: sed add space X times
Top Forums Shell Programming and Scripting sed add space X times Post 302932789 by bakunin on Saturday 24th of January 2015 03:53:21 PM
Old 01-24-2015
Quote:
Originally Posted by holyearth
I want to add X ($VARIABLE) number of spaces (which comes from RANDOM)

i.e.
VARIABLE='14'
then it will do:
Code:
sed 's/$/              /'

In short: this is not possible. The reason is that regular expressions have certain limitations and this is one of them.

If you are interested in a general and very scientific explanation of why this is so: regular expressions are of the same expressive power as regular languages, which themselves are Type-3 languages in the Chomsky-Schützenberger hierarchy. (Look up "Chomsky hierarchy" and "regular language" in Wikipedia or the net for more detail.) An expression of the form <expression> times variable would need a so-called backreference, which is not possible in a regular language because it would not be context-free any more. See the Pumping Lemma, which can be used to prove this.

Having said this: you already got some workarounds of one or the other form. Just be aware that everything you can achieve is more or less clever workarounds for the mentioned limitation.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed X amount of times - X is dynamic

I'm trying to make a bash shell script that will allow a user to modify another file based on input they give. Maybe someone can see what I'm doing wrong here. I'm still pretty new at this... Let's say my temp file contains this: 0 1 HELLO 3 4 And here's the code: old=(0 1 3 4) new=(zero... (2 Replies)
Discussion started by: Loriel
2 Replies

2. Shell Programming and Scripting

Awk script to add times.

Hey, Im trying to format the last command to tell me just the user names, logins, and the time that they were logged in. So far I got the users logins using a loop that counts the amount of times a user logged in but im not sure how to start the time array. The time im trying to use is the last... (2 Replies)
Discussion started by: Trilogie
2 Replies

3. Shell Programming and Scripting

How to print first two words two times using SED...

I have string as and I want to print first two words as output. (3 Replies)
Discussion started by: Diggi
3 Replies

4. Shell Programming and Scripting

Add white space to the end of a line with sed

Im trying to add 5 blank spaces to the end of each line in a file in a sed script. I can figure out who o put the spaces pretty much anywhere else but at the end. thanks Karl (7 Replies)
Discussion started by: karlanderson
7 Replies

5. Shell Programming and Scripting

Add missing times

Not sure about the title if someone has a better name for it please lemme know and I will edit the title. I have several (10+ files) which look something like: File 1: 12/28/2009 04:0 8 12/28/2009 04:4 4 12/28/2009 05:0 4 . . . File 2: 12/28/2009 04:1 7 12/28/2009 04:2 3... (2 Replies)
Discussion started by: jstrangfeld
2 Replies

6. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

7. Shell Programming and Scripting

Sed or Awk for lines between two strings multiple times and keep the last one

Hi, I am trying to get lines between the last occurrences of two patterns. I have files that have several occurrences of “Standard” and “Visual”. I will like to get the lines between “Standard” and “Visual” but I only want to retain only the last one e.g. Standard Some words Some words Some... (4 Replies)
Discussion started by: damanidada
4 Replies

8. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

9. Shell Programming and Scripting

Awk/sed - add space between dot and letter

I need to change . into . so that e.g. A.Jbecomes A. JI have tried sed 's/\./\.\ /g' but that didn't work. (9 Replies)
Discussion started by: locoroco
9 Replies

10. Shell Programming and Scripting

sed command to replace one value which occurs multiple times

Hi, My Input File : "MN.1.2.1.2.14.1.1" := "MN_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) "MOS.1.2.1.2.13.6.2" := "MOS_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) Like above template,I have... (4 Replies)
Discussion started by: Preeti Chandra
4 Replies
VGRINDEFS(5)						      BSD File Formats Manual						      VGRINDEFS(5)

NAME
vgrindefs -- language definition data base for vgrind(1) SYNOPSIS
vgrindefs DESCRIPTION
The vgrindefs file contains all language definitions for vgrind(1). The data base is very similar to termcap(5). FIELDS
The following table names and describes each field. Name Type Description pb str regular expression for start of a procedure bb str regular expression for start of a lexical block be str regular expression for the end of a lexical block cb str regular expression for the start of a comment ce str regular expression for the end of a comment sb str regular expression for the start of a string se str regular expression for the end of a string lb str regular expression for the start of a character constant le str regular expression for the end of a character constant tl bool present means procedures are only defined at the top lexical level oc bool present means upper and lower case are equivalent kw str a list of keywords separated by spaces EXAMPLES
The following entry, which describes the C language, is typical of a language entry. C|c::pb=^d?*?d?pd?(a?):bb={:be=}:cb=/*:ce=*/:sb=":se=e": :lb=':le=e':tl: :kw=asm auto break case char continue default do double else enum extern float for fortran goto if int long register return short sizeof static struct switch typedef union unsigned while #define #else #endif #if #ifdef #ifndef #include #undef # define else endif if ifdef ifndef include undef: Note that the first field is just the language name (and any variants of it). Thus the C language could be specified to vgrind(1) as "c" or "C". Entries may continue onto multiple lines by giving a as the last character of a line. Capabilities in vgrindefs are of two types: Boolean capabilities which indicate that the language has some particular feature and string capabilities which give a regular expression or keyword list. REGULAR EXPRESSIONS
vgrindefs uses regular expression which are very similar to those of ex(1) and lex(1). The characters `^', `$', `:' and `' are reserved characters and must be "quoted" with a preceding '' if they are to be included as normal characters. The metasymbols and their meanings are: $ the end of a line ^ the beginning of a line d a delimiter (space, tab, newline, start of line) a matches any string of symbols (like .* in lex) p matches any alphanumeric name. In a procedure definition (pb) the string that matches this symbol is used as the procedure name. () grouping | alternation ? last item is optional e preceding any string means that the string will not match an input string if the input string is preceded by an escape character (). This is typically used for languages (like C) which can include the string delimiter in a string by escaping it. Unlike other regular expressions in the system, these match words and not characters. Hence something like "(tramp|steamer)flies?" would match "tramp", "steamer", "trampflies", or "steamerflies". KEYWORD LIST
The keyword list is just a list of keywords in the language separated by spaces. If the "oc" boolean is specified, indicating that upper and lower case are equivalent, then all the keywords should be specified in lower case. FILES
/usr/share/misc/vgrindefs File containing terminal descriptions. SEE ALSO
troff(1), vgrind(1) HISTORY
The vgrindefs file format appeared in 4.2BSD. BSD
June 6, 1993 BSD
All times are GMT -4. The time now is 03:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy