Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Textverarbeitung "cut"

Empfohlene Antworten

Veröffentlicht

Hallo zusammen,

ich habe folgendes Problem.

Ich bekomme zufällig irgendwelche automatisch generierte Textdateien, welche verschiedene Namen (DatumUhrzeit) und einen internen Aufbau im Stil von TEXT|TEXT|TEXT haben.

Ich habe nun ein Script geschrieben, welches eine dieser Dateien nimmt und den Inhalt mit cut in eine variable schreibt.

Var1=`cut -d "|" -f1 $DATEI`

Wenn ich das jetzt aber in eine Datenbank schreiben möchte, schreibt er ja bei mehrzeiligen Textdateien alle Zeilen von f1 rein...

cut -b bringt mir leider nichts, da die Texte innerhalb der Spalten verschieden lang sind..

Was gibt es denn hier für einen Befehl, wie man nur die Zeile 1 cutten kann, im nächsten Schritt Zeile 2 etc?

Vielen Dank schonmal :)

LG InfinityX / Jonas

Servus InfinityX,

Deine Beschreibung ist ungenau. Bitte beschreibe Dein Anliegen so, dass man es auch nachvollziehen kann.

Auch wäre es interessant zu erfahren, wie das Script derzeit ausschaut.

  • Autor

GuMo

Bsp .txt Datei:

Status|Datum|Uhrzeit|Standort|Meldung

Status|Datum|Uhrzeit|Standort|Meldung

Status|Datum|Uhrzeit|Standort|Meldung

Wenn ich das nun mit meinem vorhandenen Script cutte und in eine Variable schreibe, bekomme ich für Spalte 1:

Status

Status

Status

Ich brauche jedoch für den Eintrag in die DB jeweils immer eine einzeilige Variable da sonst alle 3 Zeilen in die DB geschrieben werden...

Hier mein bisheriges Script:

#!/bin/bash


DATA=`ls /script/. | grep .txt | head -1`


i=1


INHALT=`cat /script/$DATA | wc -l`


while [ $i -le $INHALT ]

do


		i=$(($i+1))


		status=`cut -d "|" -f1 $DATA`

		datum=`cut -d "|" -f2 $DATA`

		uhrzeit=`cut -d "|" -f3 $DATA`

		standort=`cut -d "|" -f4 $DATA`

		meldung=`cut -d "|" -f5 $DATA`


mysql -u USER --password=PW  << EOFMYSQL


INSERT INTO DB.Meldungen (Status, Datum, Uhrzeit, Standort, Meldung) VALUES ('$status', '$datum', '$uhrzeit', '$standort', '$meldung');

EOFMYSQL



done


echo "Data" $DATA;

echo "Inhalt" $INHALT;

echo "anzahl" $ANZAHL;

echo "status" $status;

echo "datum" $datum;

echo "uhrzeit" $uhrzeit;

echo "standort" $standort;

echo "meldung" $meldung;

echo "i" $i;


mv $DATA ./used/$DATA

LG

  • Autor

Hat sich erledigt...

Lösung:

IFS=$'\012'


for i in $(cat $DATA);

do


		status=$(echo $i cut -d "|" -f1 | tr "$IFS" '\n')

		datum=$(echo $i cut -d "|" -f2 | tr "$IFS" '\n')

		uhrzeit=$(echo $i cut -d "|" -f3 | tr "$IFS" '\n')

		standort=$(echo $i cut -d "|" -f4 | tr "$IFS" '\n')

		meldung=$(echo $i cut -d "|" -f5 | tr "$IFS" '\n')


mysql -u USER --password=PW  << EOFMYSQL


INSERT INTO DB.Meldungen (Status, Datum, Uhrzeit, Standort, Meldung) VALUES ('$status', '$datum', '$uhrzeit', '$standort', '$meldung');

EOFMYSQL



done

trotzdem Danke

LG

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.