insert a space after a specific character with awk? - awk

How do I insert a space after a specific character with awk?
The test data
<>jhjashdhasdkh
I want to insert a space " " after > so the data string would become...
The modifyed test data now becomes...
<> jhjashdhasdkh
Does anyone know how to do that with awk. ? I've tried everything I can..
Thank you for helping,

You'll want to read about the sub and gsub functions in the gawk manual

In my case, I had to insert a decimal point after a number.
NewBid = substr(spotBidRate,1,1) "." substr(spotBidRate, 2)
The sample input: spotBidRate=367305000 The Sample output: NewBid=3.67305000
So in your case, it would be a space, NewBid = substr(spotBidRate,1,1) " " substr(spotBidRate, 2) Hope that helps!

Related

Pandas use contains for a specific word excluding similar words

I am filtering a string using Result[Results['Subject'].str.contains('lock')] but I need to esclude words like "clock"
What I need is the sting staring with "lock", ending as " lock" or containing " lock "
Many thanks
Use a regex word delimiter \b:
Results[Results['Subject'].str.contains(r'\block\b')]
Example input:
Results = pd.DataFrame({'Subject': ['lock', 'clock', 'abc lock', 'locker']})
Output:
Subject
0 lock
2 abc lock

Length of second csv file not equal to base file using np.select

I am trying to use np.select to merge the values of two columns into a new column from file1 and write it into file2. However the length (amount of rows) is not the same in both files at the end. For some reason, there are always a couple of hundred of lines missing.
I have three columns: 'Time', "LSB", "MSB" with each about 1000 values
Here is an extract of my code:
file2 = File_Name_G() #This is a function to define the name of file2
imu_gyr = pd.read_csv(file1, index_col=0)
imu_gyr["Gyr_x"] = np.select([(imu_gyr["LSB"] + imu_gyr["MSB"]*256) > 32767], [(imu_gyr["LSB"] + imu_gyr["MSB"]*256 - 65536)/1000], (imu_gyr["LSB"] + imu_gyr["MSB"]*256)/1000]
imu_gyr.to_csv(file2)
My code shows no errors and I am really confused as to why this is happening. I thought it could have something to do with processing power, but the length of my file1 has no real influence on the amount of lines missing in file2.
Has anyone had a similar problem or can someone help me solve this?
Thanks a lot! :)
Sam

How do I need to change the datatype in a textfile in order to read a String and not a Integer (C++/CLI, OleDb)?

My goal is it to read from a text file. This text file contains different columns and rows for each value. I can read the file as long as I don't change the datatype that windows set on its own. But I do not want the "plz" and "nr" column to be numbers (integers) but a text (String) value because a plz could contain values like "01979" and the nr could contain something like "4a". As a number the starting zero would be lost and this way something like a postcard would never reach its intended destination.
This way I need to change the datatype in a "schema.ini" file. But it doesn't work. I think I make some mistakes and do not follow the tutorial the way I need to do: "Schema.ini File"
Everytime I tried to read a String I got an Exception because it still want to read an Int32-values that I would need to convert into a string.
I did name the file "kunde.txt"
knr|nachname|vorname|plz|ort|strasse|nr
1|Müller|Johan|12345|Muster|Musterstr|1
2|Kummer|Freude|23456|Feeling|Gefühlswelt|4a
Col 0 = knr, 1 = nachname, 2 = vorname, 3 = plz, 4 = ort, 5 = strasse, 6 = nr
con->ConnectionString =
"Provider=Microsoft.JET.OLEDB.4.0;" +
"Data Source=D:/C++/Quellen;" +
"Extended Properties=text";
// ....
meineKunden->CommandText =
"SELECT knr, nachname, vorname, plz, ort, strasse, nr " +
"FROM kunde.txt ";
// ....
String ^ str;
while(reader->Read()){
str += Convert::ToString(reader->GetInt32(0));
str += " ";
str += reader->GetString(3);
str += " ";
str += reader->GetString(6);
str += "\r\n";
}
this->txb_Insert->Text = str;
My schema.ini
[kunde.txt]
ColNameHeader=True
Format=Delimited(|)
3=plz Char Width 5
6=nr Char Width 10
I did try with "Col3" instead of "3". I did use "Text" rather tan "Char", and I did even attempted it without Width. But everytime I got the same failure message. Even if i use 4 or 7 ... since I am not sure how it will be counted in the ini / txt file.
Exception:
System.InvalidCastException: Die angegebene Umwandlung ist ungültig.
bei System.Data.OleDb.ColumnBinding.ValueString()
bei System.Data.OleDb.OleDbDataReader.GetString(Int32 ordinal)
The exception is calls already by str += reader->GetString(3)
If I am correct column 3 contains plz, this way.
Could someone please say what I do understand wrong with the schema.ini file?
Since I could read the file without mistakes as long as I doesn't try to specific change the datatype in some columns the problems need to be with the ini file. At least I think so.
EDIT: I did change my ini-file to:
[kunde.txt]
ColNameHeader=True
Format=Delimited(|)
Col1="knr" Integer
Col2="nachname" Text
Col3="vorname" Text
Col4="plz" Text
Col5="ort" Text
Col6="strasse" Text
Col7="nr" Text
Now it works for "plz" but the exception is called in the last row, when I call the "nr". WTF?
You have to maintain that the way you read data from the file suits the way it's written in .
So if the file in not written by you and you have to use it,try to know if it has a specific structure (for ex: fixed length record delimited fields , fixed length record fixed length fields.. etc) and use a way that suits this structure to read it.
Also try to know how fields of records are written in details because the way you read is the same as you write .

Find Each Occurrence of X and Insert a Carriage Return

A colleague has some data he is putting into a flat file (.txt) and needs to insert a carriage return before EACH occurrence of 'POL01', 'SUB01','VEH01','MCO01'.
I did use:
For Each line1 As String In System.IO.File.ReadAllLines(BodyFileLoc)
If line1.Contains("POL01") Or line1.Contains("SUB01") Or line1.Contains("VEH01") Or line1.Contains("MCO01") Then
Writer.WriteLine(Environment.NewLine & line1)
Else
Writer.WriteLine(line1)
End If
Next
But unfortunately it turns out that the file is not formatted in 'lines' by SSIS but as one whole string.
How can I insert a carriage return before every occurrence of the above?
Test Text
POL01CALT302276F 332 NBPM 00101 20151113201511130001201611132359 2015111300010020151113000100SUB01CALT302276F 332 NBPMP01 Akl Abi-Khalil 19670131 M U33 Stoford Close SW19 6TJ 2015111300010020151113000100VEH01CALT302276F 332 NBPM001LV56 LEJ N 2006VAUXHALL CA 2015111300010020151113000100MCO01CALT302276F 332 NBPM0101 0 2015111300010020151113000100POL01CALT742569N
You can use regular expressions for this, specifically by using Regex.Replace to find and replace each occurrence of the strings you're looking for with a newline followed by the matching text:
Dim str as String = "xxxPOL01xxxSUB01xxxVEH01xxxMCO01xxx"
Dim output as String = Regex.Replace(str, "((?:POL|SUB|VEH|MCO)01)", Environment.NewLine + "$1")
'output contains:
'xxx
'POL01xxx
'SUB01xxx
'VEH01xxx
'MCO01xxx
There may be a better way to construct this regular expression, but this is a simple alternation on the different letters, followed by 01. This matched text is represented by the $1 in the replacement string.
If you're new to regular expressions, there are a number of tools that help you understand them - for example, regex101.com will show you an explanation of the one I have used here:

Vi: how to automatically insert spaces

I'm trying to write a nice feature for crazy people like me who like there lines to be perfectly aligned.
I often write some file in which the format is "key = value".
Since the key may contain an indeterminate number of character, one have to manually align the "=" symbols which is not cool.
Is there a way to tell vi "when someone type the equal character, then insert as spaces as necessary to go to the column 25, then write an the equal symbol"?
The second step will be to define a shortcut to apply this format to an entire file.
Any help would be appreciated.
Ben.
Map the behavior of = in Insert Mode.
Next code will add spaces until column 24 from current cursor position and will add an equal sign after it. If there were characters after cursor position (suppose in a middle of a word), those characters will be moved after column 25. Add it to your vimrc file and try.
"" If length of the line is more or equal to 24, add an equal sign at the end.
"" Otherwise insert spaces from current position of cursor until column 24
"" and an equal sign, moving characters after it.
function My_align()
let line_len = strlen( getline('.') )
if line_len >= 24
s/$/=/
return
endif
let col_pos = col('.')
exe 's/\%#\(.\|$\)/\=submatch(1) . printf( "%' . (24 - col_pos) . 's%s", " ", "=" )/'
endfunction
inoremap = <Esc>:call My_align()<CR>A
For second step, use the multiple repeats command, check for an equal sign and insert spaces until column 25 just before it. Won't work if equal sign is after column 25 before executing it, but you get the idea.
:g/=/exe 's/=/\=printf( "%' . ( 24 - stridx( getline('.'), "=" ) ) . 's", " " ) . submatch(0)/'