Remove first 4 whitespaces from line - awk

I inherited a script I want to refactor.
for some reason the script is intended with four whitespaces.
I would like to remove leading four whitespaces from each line.
Is there any handy and fast way to do it?
Message "Try to get Package Lock..."
if waitForPackageLock("300","false")
comment "UCS: Extra check if package lock is available."
endif
Cheers

That was a difficult one.
sed 's/^ //'

Or edit your script with an editor.
vim file
:%<
: execute a command
% apply to all lines
< remove one indent

Related

Reformat as single line

I would like to be able to remove whitespace and newlines for a block of code/text.
A single line is sometimes easier to copy paste into cli tools.
I currently do this using find and replace on whitespace and newlines, but is there a faster way?
Found this after a bit of digging in the keymap:
Join lines: Ctrl+Shift+J
It even handles multi-line comments.

AIX: remove the last symbols (CRLF) from a file

There is a large file where the last symbols are \r\n. I need to remove them. It seems to be equivalent to removing the last line(?).
UPD: no, it's not: a file have only one line, which ends with \r\n.
I know two ways, but both don't work for AIX:
sed 's/\r\n$//' file # I don't why it doesn't work
head -c-2 # head doesn't work with negative numbers
Is there any solution for AIX? A lot of large files must be processed, so performance is important.
Usually, if you need to edit a file via a script in place, I use ed due to historical reasons. For example:
ed - /tmp/foo.txt <<EOF
g/^$/d
w
q
EOF
ed is more than a bit cantankerous. Note also that you did not really remove the empty lines at the bottom of the file but rather all of the empty lines. With ed and some practice you can probably achieve deleting only the empty lines at the bottom of the file. e.g. go to the bottom of the file, search up for a non-empty line, then move down a line and delete from that point to the end of the file. ed command scripts act (pretty much) as you would expect.
Also, if they really do have \r\n, then those are not going to be considered empty lines but rather lines with a control-M (\r) in them. You may need to adjust your pattern if that is the case.
My answer https://stackoverflow.com/a/46083912/3220113 to the duplicate question should work here too. Another solution is using
awk ' (NR>1) { print s }
{s=$0}
END { printf("%s",substr($2, 1, length($2)-1) ) }
' inputfile

Using SED to match emails in a sql dump and replace them

I am trying to scrub the emails from a SQL dump file, and I could use some advice. I am doing this because I want to send some developers "mostly correct" information, without sharing actual user information. I have a BASH script that loops through line-by-line, so I am trying to do a SED replace on the INSERT statements. I need to iterate through the dumb because I have some other scrubbing stuff, which is working. I have some regex that works (I think), but I cannot seem to get it into SED. The regex of:
'(.*#.*?)'
Will match 'emailname#emaildomain.com', but I'm having trouble getting it into SED, and I'm sure that there is a better REGEX. Here's my example line.
'firstname','emailname#emaildomain.com','lastname'
I hope to be able to replace whenever I have an # between quotes with 'empty#invalid'. Any advice would be greatly appreciated.
Try:
sed "s/'[^#']*#[^#']*'/'empty#invalid'/g"
I've replaced your .* with the more specific [*#']*, which only matches strings which don't have any single-quotes ' or #, which is needed because sed is greedy.

Is there a way to reformat braces automatically with Vim?

I would like to reformat some code which looks like this :
if (cond) {
foo;
}
to
if (cond)
{
foo;
}
Since this is C code, I have been looking at cindent/cinoptions to use with = but it seems it does not deal with multiline rules.
I have been looking at formatoptionsto use with gq, and it does not seem to be possible either.
So is it possible using default Vim options or should I use a specific plugin or function ?
:%s/^\(\s*\).*\zs{\s*$/\r\1{/
Breakdown:
^\(\s*\) = capture the whitespace at the beginning of the line
.* = everything else
\zs = start replacement after this
{ = open curly brace
\s*$ = trailing whitespace before line end
\r\1{ = newline, captured whitespace, brace
I don't know if this completely solves your problem, but if this is a one-shot operation, you might want to try regular expressions:
:%s/^\(\s*\)\(.*)\)\s*{\s*$/\1\2^M\1{/
Note that ^M is a control character that is usually generated (depending on your terminal) by pressing CTRL-V followed by ENTER.
EDIT: As pointed out in the comments by Jay and Zyx, \r is a better way of inserting a line break into the replaced string. I wasn't aware of that, many thanks for the hint.
If you install Artistic Style you can do something like:
:set formatprg=astyle\ -b
Then use gq to reformat chunks of code.emphasized text
If you want this enabled every time you edit a C file,
you can add the following to your .vimrc file.
autocmd BufNewFile,BufRead *.c set formatprg=astyle\ -b
I don't know if you can do it within vim itself, but you can try the BSD indent command with the -bl option. With the cursor on the first {, you can type !%indent -blEnter.

Is there a tool to clean the output of the script(1) tool?

script(1) is a tool for keeping a record of an interactive terminal session; by default it writes to the file transcript. My problem is that I use ksh93, which has readline features, and so the transcript is mucked up with all sorts of terminal escape sequences and it can be very difficult to reconstruct the command that was actually executed. Not to mention the stray ^M's and the like.
I'm looking for a tool that will read a transcript file written by script, remove all the junk, and reconstruct what the shell thought it was executing, so I have something that shows $PS1 and the commands actually executed. Failing that, I'm looking for suggestions on how to write such a tool, ideally using knowledge from the terminfo database, or failing that, just using ANSI escape sequences.
A cheat that looks in shell history, as long as it really really works, would also be acceptable.
Doesn't cat/more work by default for browsing the transcript? Do you intend to create a script out of the commands actually executed (which in my experience can be dangerous)?
Anyway, 3 years without an answer, so I will give it a shot with an incomplete solution. If your are only interested in the commands actually typed, remove the non-printable characters, then replace PS1' with something readable and unique, and grep for that unique string. Like this:
$ sed -i 's/[^[:print:]]//g' transcript
$ sed 's/]0;cartman#southpark: ~cartman#southpark:~/CARTMAN/g' transcript | grep CARTMAN
Explanation: After first sed, PS1' can be taken from one of the first few lines of the transcript file, as is -- PS1' is different from PS1 -- and can be modified with a unique readable string ("CARTMAN" here). Note that the dollar sign at the end of the prompt was left out intentionally.
In the few examples that I tried, this didn't solve everything but took care of most issues.
This is essentially the same question asked recently in Can I programmatically “burn in” ANSI control codes to a file using unix utils? -- removing all nonprinting characters will not fix
embedded escape sequences
backspace/overstriking for underlining
use of carriage-returns for overstriking